← Back to Exceptions
Question 756
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= 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.