USER DEFINE EXCEPTION

How to Make Over Exception


Example 1:

 
                
             public class AgeNegativeException extends Exception
             {
                public AgeNegativeException(String str) 
                {
                   super (str);  
                }
                public static void main(String[] args) 
                {
                   Scanner sc  = new Scanner(System.in);
                   System.out.println("Enter age");
                   int age  = sc.nextInt();
                   if(age>0)
                   {
                      System.out.println("Age  : "+age);      
                   }
                   else
                   {
                      try
                      {
                         throw new AgeNegativeException("age negative not possible");
                      }
                      catch (AgeNegativeException ex)
                      {
                         ex.printStackTrace();   
                      }
                   }
                }
             }                

Output:

     Enter age
     
     

Example 2:

 
                
             import java.io.*;
             public class CheckingAccount
             {
                private double balance;
                private int number;
                public CheckingAccount(int number)
                {
                   this.number = number;
                }
                public void deposit(double amount)
                {
                   balance += amount;
                }
                public void withdraw(double amount) throws   InsufficientFundsException
                {
                   if (amount <= balance)
                   {
                      balance -= amount;
                   }
                   else
                   {
                      double needs = amount - balance;
                      throw new InsufficientFundsException(needs);
                   }
                }
                public  double getBalance()
                {
                   return balance;
                }
                public int getNumber()
                {
                   return number;
                }
             }
            
             //File Name InsufficientFundsException.java
             import java.io.*;
             public class InsufficientFundsException extends Exception
             {
                private double amount;
                public InsufficientFundsException(double amount)
                {
                   this.amount = amount;
                } 
                public double getAmount()
                {
                   return amount;
                }
             }
                         
             package com.pack1;
             //File Name BankDemo.java
             public class BankDemo
             {
                public static void main(String [] args)
                {
                   CheckingAccount c = new CheckingAccount(101);
                   System.out.println("Depositing 500...");
                   c.deposit(500.00);
                   try
                   {
                      System.out.println("\nWithdrawing 100...");
                      c.withdraw(100.00);
                      System.out.println("\nWithdrawing 600...");
                      c.withdraw(600.00);
                   }
                   catch (InsufficientFundsException e)
                   {
                      System.out.println("Sorry, but you are short "+ e.getAmount());
                      e.printStackTrace();
                   }
                }
             }                

Output:

     Depositing $500...
     Withdrawing $100...
     Withdrawing $600...
     Sorry, but you are short $200.0