โ Back to Exceptions
Question 666
Function That Throws
Function That Throws
Read two integers and printResult: <Divide(a, b)>. Write a local functionint Divide(int x, int y)that throwsnew DivideByZeroException("cannot divide by zero")whenyis0, and otherwise returnsx / y. Catch that exception in the caller and print its message. Use top-level statements (declare the local function at the end). Input10then2prints: Result: 5 Input10then0prints: cannot divide by zero
Expected Output:
Result: 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.