← Back to Miscellaneous
Question 219
Checked Exception and throws
Checked Exception and throws
Create a checked exception and use it: 1. DefineInsufficientFundsExceptionthat extendsExceptionwith a constructor that accepts a message string. 2. Writewithdraw(double balance, double amount) throws InsufficientFundsException— throw the exception with message"Not enough funds"ifamount > balance. 3. Inmain, read balance and amount. Callwithdraw, print"Withdrawal successful"or"Error: " + e.getMessage()if caught. BecauseInsufficientFundsExceptionextendsException(notRuntimeException),withdrawmust declarethrows. ⚠️ AUTO-INPUT MODE: The input stream will automatically contain a balance and an amount, for example'50.0 100.0\n'. Your code is also run with a balance that covers the amount, so both messages must work.
Expected Output:
Error: Not enough funds= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Miscellaneous
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.