Exception Handling

InsufficientFundsException.java
class InsufficientFundsException extends Exception{
private double amount;

   //Handle Exception:-
   public InsufficientFundsException(double amount){
     this.amount=amount;
   }

   //getAmount:-
   public double getAmount(){
      return amount;
   }


}//class InsufficientFundsException 

CheckingAccount.java
class CheckingAccount{
private double balance;
private int number;

   //Constructor to Initialize new account:-
     public CheckingAccount(int number){
        this.number=number;
     }

   //method to deposit money:-
     public void deposit(double amount){
        this.balance+=amount;
     }  
   //method to withdraw money:-
     public void withdraw(double amount) throws InsufficientFundsException {
        if(amount<=balance){
           balance-=amount;   
}else{
//low balance high input:-
   double needs=amount-balance;
   throw new InsufficientFundsException(needs);
        }//else condition closed
     }//withdraw()

     //get account balance:-
       public double getBalance(){
          return balance;
       }

     //get account number:-
       public int getNumber(){
          return number;
       }


}//class CheckingAccount

BankDemo.java
class BankDemo{
   public static void main(String[] args){
//Initializing new account:-
        CheckingAccount c1=new CheckingAccount(101);

//Depositing money:-
System.out.println("Depositing $: 500");
        c1.deposit(500.00);//depositing 500
        System.out.println("Account Balance is: " + c1.getBalance());      
  
try{
   //withdrawing money:-
   System.out.println("\n Withdrawing $: 100");
           c1.withdraw(100.00);//c1 is withdrawing
           System.out.println("Account Balance is: " + c1.getBalance());    
   
//Again withdrawing money:-
           System.out.println("\n Withdrawing $: 600");
           c1.withdraw(600.00);//c1 is withdrawing
   System.out.println("Account Balance is: " + c1.getBalance());        

//catch exceptio here:-
}catch(InsufficientFundsException e){
           System.out.println("Sorry, You are short around at $: " + e.getAmount());
           e.printStackTrace();
}//catch block closed   

   }//main()

}//class BankDemo













ClassNotFoundException:

For example, the below program will throw ClassNotFoundException if the mentioned class “oracle.jdbc.driver.OracleDriver” is not found in the classpath.

public class MainClass
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}

o/p:-

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver



NoClassDefFoundError:

The required class definition was present at compile time, but it was missing at runtime.
class A
{
  // some code
}
public class B
{
    public static void main(String[] args)
    {
        A a = new A();
    }
}

When you compile the above program, two .class files will be generated. One is A.class and another one is B.class. If you remove the A.class file and run the B.class file, Java Runtime System will throw NoClassDefFoundError like below:

o/p:-

Exception in thread "main" java.lang.NoClassDefFoundError: A

No comments:

Post a Comment