Question 15
Consider the Java code given below.
class NegativeBalanceException extends Exception {
public NegativeBalanceException() {
super("Transaction failed: negative balance");
}
}
class BankAccount {
private double balance;
public BankAccount(double b) {
balance = b;
}
public void withdraw(double amount) throws NegativeBalanceException {
if(balance - amount < 0)
throw new NegativeBalanceException();
balance -= amount;
System.out.println("Withdrawal successful: " + balance);
}
}
public class TestBank {
public static void main(String[] args) {
try {
BankAccount acc1 = new BankAccount(500);
BankAccount acc2 = new BankAccount(200);
acc1.withdraw(600);
acc2.withdraw(150);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Choose the correct option.
This program generates output:
Transaction failed: negative balanceThis program generates output:
Transaction failed: negative balance
Withdrawal successful: 50.0This program generates output:
Withdrawal successful: -100.0
Withdrawal successful: 50.0The program crashes due to the uncaught exception: NegativeBalanceException