CJCoding With Joseph

Function That Throws

Read two integers and print Result: <Divide(a, b)>. Write a local function int Divide(int x, int y) that throws new DivideByZeroException("cannot divide by zero") when y is 0, and otherwise returns x / y. Catch that exception in the caller and print its message. Use top-level statements (declare the local function at the end).

Input 10 then 2 prints: Result: 5
Input 10 then 0 prints: cannot divide by zero

Expected Output:

Result: 5
Topics:
Exceptions
๐Ÿ“ฅ Auto-Input:
10\n2\n
This input is automatically fed to your program's stdin
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.