Error and Exception Handling · 15 / 20 Exercise Medium

Custom exception

Objective

Create a Python program with a custom exception to control withdrawals of money from an account.

Instructions

  • Create the class InsufficientFundsError, which inherits from Exception. Its message is Insufficient funds: missing N, where N is the money that is missing.
  • Create the class Account with the attribute balance and the method withdraw(amount), which raises the exception if the amount is greater than the balance and, otherwise, subtracts the amount.
  • Read the initial balance and the amount to withdraw (integers).
  • If the withdrawal is made, show New balance: X. If the exception is raised, show its message.
Input
100
30
Output
New balance: 70

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise