โ Back to Exceptions
Question 664
Wrap With Inner Exception
Wrap With Inner Exception
Use a nestedtry/catch. Inner try: readn; ifn < 0,throw new ArgumentException("negative value"); otherwise printValue: <n>. The inner catch wraps it:throw new InvalidOperationException("validation failed", ex), passing the original as the inner exception. The outer catch printsex.Message, then printsCaused by: <ex.InnerException.Message>. Use top-level statements. Input5prints: Value: 5 Input-2prints: validation failed Caused by: negative value
Expected Output:
Value: 5
Topics:
Exceptions
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท 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.