Catch An Invalid Cast
mediumQuestion 790An object variable can hold any type, but unboxing it to the wrong type throws an InvalidCastException. Read one line from input. If the line is exactly num, store the boxed integer 7 in an object; otherwise store the string "seven" in it. Then try to unbox it with (int)obj. If the cast succeeds, print Value: <n> (for example Value: 7). If it throws InvalidCastException, print Error: bad cast. Use top-level statements with try/catch (no Main method needed).
Input num prints: Value: 7 Input seven prints: Error: bad cast
Expected output
Value: 7= newline. The output ends with a newline, so 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.