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: