0% found this document useful (0 votes)
20 views4 pages

Java Custom Exceptions in Banking System

The document presents a Java program that defines custom exceptions for invalid age and insufficient balance in a bank account context. It includes a BankAccount class with methods for account creation, withdrawal, and displaying account details, while handling exceptions appropriately. The main method demonstrates the usage of these classes and exception handling with sample accounts.

Uploaded by

Ashok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Java Custom Exceptions in Banking System

The document presents a Java program that defines custom exceptions for invalid age and insufficient balance in a bank account context. It includes a BankAccount class with methods for account creation, withdrawal, and displaying account details, while handling exceptions appropriately. The main method demonstrates the usage of these classes and exception handling with sample accounts.

Uploaded by

Ashok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program No: 3

Name:- Akanksha Kagwade


Roll No:- 03
Batch:- A1

Program:

class InvalidAgeException extends Exception


{
public InvalidAgeException(String message)
{
super (message);
}
}
class InsufficientBalanceException extends Exception
{
public InsufficientBalanceException(String message)
{
super (message);

}
}
class BankAccount
{
private String name;
private int age;
private double balance;

public BankAccount(String name, int age, double balance) throws


InvalidAgeException
{
if (age < 18)
{
throw new InvalidAgeException("Age must be 18 or above to
open a bank account.");
}
[Link]=name;
[Link]=age;
[Link]=balance;
}
public void withdraw(double amount) throws
InsufficientBalanceException
{
if(amount < balance)
{
throw new InsufficientBalanceException("Insufficint balnce cannot
withdraw:"+amount);
}
balance-=amount;
[Link]("withdrawal successful. remaining
balance:"+balance);
}
public void displayAccount(){
[Link]("Account holder:"+name);
[Link]("Age:"+age);
[Link]("Balance:"+balance);
}
}
public class userDefinedExceptionDemo{
public static void main(String args[]){
try{
BankAccount account1= new BankAccount("John doe",16, 5000
);
} catch (InvalidAgeException e){
[Link]("Exception:"+[Link]());
}
try{
BankAccount account2= new BankAccount("Alice",25, 10000);
[Link]();
[Link](12000);
}
catch(InvalidAgeException | InsufficientBalanceException e)
{
[Link]("Exception:"+[Link]());

}
}
}
Output:

You might also like