-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path06_custom_exceptions.py
More file actions
28 lines (24 loc) · 973 Bytes
/
Copy path06_custom_exceptions.py
File metadata and controls
28 lines (24 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class InsufficientFundsError(Exception):
"""Custom exception for bank account errors."""
def __init__(self, balance, amount):
self.message = f"Attempted to withdraw ${amount} but balance is only ${balance}."
super().__init__(self.message)
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
def withdraw(self, amount):
if amount > self.balance:
raise InsufficientFundsError(self.balance, amount)
self.balance -= amount
print(f"Withdrew ${amount}. New balance: ${self.balance}")
# Testing the implementation
if __name__ == "__main__":
acc = BankAccount("Krish", 1000)
try:
acc.withdraw(500)
acc.withdraw(700) # This should trigger the custom exception
except InsufficientFundsError as e:
print(f"Transaction Failed: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")