← Back to Exceptions
Question 790
Catch An Invalid Cast
Catch An Invalid Cast
Anobjectvariable can hold any type, but unboxing it to the wrong type throws anInvalidCastException. Read one line from input. If the line is exactlynum, store the boxed integer7in anobject; otherwise store the string"seven"in it. Then try to unbox it with(int)obj. If the cast succeeds, printValue: <n>(for exampleValue: 7). If it throwsInvalidCastException, printError: bad cast. Use top-level statements withtry/catch(noMainmethod needed). Input num prints: Value: 7 Input seven prints: Error: bad cast
Expected Output:
Value: 7= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Exceptions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cw, cr, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.