Bank Exception.java
Bank Exception.java
Scanner;
super(message);
super(message);
this.accountNumber = accountNumber;
this.balance = balance;
balance -= amount;
return balance;
return accountNumber;
class BankApp {
try {
if (enteredAccountNumber.equals(account1.getAccountNumber())) {
account = account1;
} else if (enteredAccountNumber.equals(account2.getAccountNumber())) {
account = account2;
if (account == null) {
// Attempt withdrawal
account.withdraw(withdrawalAmount);
} catch (InvalidAccountNumberException e) {
} catch (InsufficientFundsException e) {
} catch (Exception e) {
} finally {
scanner.close();
} }}
Explanation:
1. Custom Exceptions:
2. BankAccount Class:
o This class represents a bank account with methods to withdraw money (withdraw())
and to get account details like balance and account number.
3. BankApp Class:
o This is the main application that simulates a user interacting with the bank.
o First, the program asks the user for an account number and checks if it's valid.
o Then, it asks the user for the withdrawal amount. If the amount is greater than the
balance, it throws an InsufficientFundsException.
o The program uses a try-catch block to handle exceptions and provide feedback to the
user.
o The finally block ensures that the Scanner resource is closed, preventing resource
leaks.
Example Execution: