Design an InsufficientFunds Error

Medium ⏱ 12 min 63% acceptance ★★★★★ 4.8
Define a custom exception InsufficientFundsError(Exception) that stores the shortfall amount, then write withdraw(balance, amount) returning the new balance or raising that error with the shortfall when amount > balance. The caller should be able to read err.shortfall.

Examples

Example 1
Input
withdraw(100, 250)
Output
raises InsufficientFundsError with shortfall == 150
Explanation

250 exceeds the balance by 150.

Constraints

  • Subclass Exception.
  • The instance must expose .shortfall.

Topics

Exception Handlingcustom exceptions

Companies

HDFC BankGoldman SachsStripe

Hints

Hint 1

Override __init__, call super().__init__(msg), store the attribute.

Hint 2

raise InsufficientFundsError(amount - balance).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.