Custom Exception Creator - Problem
Create custom exception classes for a banking application that can handle different types of errors that occur during banking operations.
Requirements:
- Create
InsufficientFundsErrorexception for when account balance is too low - Create
InvalidAmountErrorexception for negative or zero transaction amounts - Create
AccountLockedErrorexception for when account is locked/frozen
Each exception should:
- Accept a custom error message in the constructor
- Have a default message if none is provided
- Be properly structured as exception classes
Implement a BankAccount class that uses these exceptions and a function that demonstrates their usage by attempting various operations and catching the exceptions.
Input & Output
Example 1 — Successful Withdrawal
$
Input:
account_balance = 100, transaction_amount = 50
›
Output:
Success: Balance is now 50
💡 Note:
Account has sufficient funds ($100), withdrawal of $50 succeeds, leaving balance of $50
Example 2 — Insufficient Funds
$
Input:
account_balance = 100, transaction_amount = 150
›
Output:
InsufficientFundsError: Insufficient funds in account
💡 Note:
Account balance ($100) is less than withdrawal amount ($150), triggers InsufficientFundsError
Example 3 — Invalid Amount
$
Input:
account_balance = 100, transaction_amount = -50
›
Output:
InvalidAmountError: Invalid transaction amount
💡 Note:
Negative transaction amount (-$50) is invalid, triggers InvalidAmountError
Constraints
- 1 ≤ account_balance ≤ 106
- -1000 ≤ transaction_amount ≤ 106
- Exception classes must inherit from base Exception class
- Each exception must have default and custom message constructors
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code