Example of user defined exception

Here we explain the user defined exception. Some time we need to define own exception class for special purpose use to extends the basic exception given by java.We define the user defined class which extends the Exception and RunableException class given by java.


Let us see it  with example. Suppose that there is a Account class and we have to show the exception of Invailid Account No and Insufficient Fund. So we defined two exception class one InvalidAccNo.java which extends Exception and another is InsufficientFund.java which extends RunableException. These two classes are user defined exception class.

Now there is a Account.java class in which we throw these two user defined exception. There is a another class called UserDefineException.java which is for testing purpose.

Now we can see each class one bye one:


1. InvalidAccNo.java

package com.javaforecast4u;
public class InvalidAccNo extends Exception{
int accno;
InvalidAccNo(){}
InvalidAccNo(int accno){
    this.accno=accno;
    }
public String getMessage(){
    String msg=null;
    if(accno==0){
         msg="Account No is not available";
    }
    else{
         msg="Accont No:"+accno+"is not available";
    }
    return msg;
}
public String toString(){
    return "Invalid Accont No exception:"+getMessage();
}
}


2. InsufficientFund.java

package com.javaforecast4u;

public class InsufficientFund extends RuntimeException {
    int bal;
    InsufficientFund(){   }
    InsufficientFund(int bal){
         this.bal=bal;
    }
public String getMessage(){
    String msg=null;
    if(bal==0){
         msg="Fund is not sufficient";
         }
    else{
         msg="You don't have amount:"+bal;
    }
    return msg;
}
public String toString(){
    return "Insuffient Funds:"+getMessage();
}
}

3. Account.java

package com.javaforecast4u;

public class Account {
    int bal=5000;
    int getBal(int accno){
         try{
             if(accno>200){
                 throw new InvalidAccNo();
             }
             else{
                 return bal;
             }
         }catch(Exception e){
             System.out.println(e);
             System.out.println(e.getMessage());
             return bal;
         }
    }
    void withdraw(int accno, int amt){
         try{
             if(accno>200){
                 throw new InvalidAccNo();
             }
             else{
                 if(amt+1000>bal){
                      throw new InsufficientFund(amt);
                 }
                 else{
                      bal=bal-amt;
                 }
             }
         }
             catch(Exception e){
                 System.out.println(e);
                 System.out.println(e.getMessage());
             }
         }
    }
   
4. UserDefineException.java
 
package com.javaforecast4u;

public class UserDefineException {
    public static void main(String as[]){
         try{
             int accno=Integer.parseInt(as[0]);
             int amt=Integer.parseInt(as[1]);
             Account acc=new Account();
             System.out.println(acc.getBal(accno));
             acc.withdraw(accno, amt);
             System.out.println(acc.getBal(accno));
         }
         catch(Exception e){
             System.out.println(e);
             System.out.println(e.getMessage());
         }
    }

}








0 comments :

Post a Comment