class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
raise ValueError("Insufficient funds")
self.balance -= amount
def get_balance(self):
return self.balance
class Transaction:
def __init__(self, account, amount):
self.account = account
self.amount = amount
def execute(self):
try:
self.account.withdraw(self.amount)
except ValueError as e:
print(f"Error: {e}")
def main():
account = BankAccount("123456")
account.deposit(1000)
transaction = Transaction(account, 500)
transaction.execute()
print(f"Account balance: ${account.get_balance():.2f}")
main()
The bug in this code is in the Transaction class. The execute() method tries to execute the transaction by calling withdraw() on the account object, but if the withdrawal fails (e.g., because there are insufficient funds), it catches the ValueError exception and prints an error message. However, it does not actually handle the error by rolling back the transaction or re-trying the withdrawal.
This bug can lead to unexpected behavior and potential data loss, as the account balance may be updated incorrectly.
Can you spot this bug and fix it?
Hilary is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.