Ads Sponsored By Google :)

Tuesday 29 January 2013

custom|user defined exception|handling in java example sample|program for|Code


user defined exception handling in java  user defined exception handling in java example user defined exception handling program in java user defined exceptions in java example user defined exception in java example program user defined exception in java sample code user defined exception in java example program user defined exception handling in java example user defined exception handling program in java java program for user defined exception handling  program for user defined exception handling  user defined exception in java example code custom defined exception handling in java example program user defined exception in java example user defined exception in java example program user defined exception in java example program code custom defined exception in java example custom defined exception in java example program 




     In order to develop user defined Exception Handling based applications, as a programmer, 
   we come across three stages they are.
    
      1)     User defined Exception sub classes
2)     User defined General / Common classes
3)     User defined Specific Classes

      For Example:

      1)  Development of user defined Exception sub classes:
                    Example:
                         Develop a User defined exception subclass for detailing with negative salary of an 
                        employee

//NsalException.java
 Package javajavax.lang;
 Public class NsalException extends Exception{
    Public NsalException(String s) { 
     Super(s);
    }
}//NsalException-------------user defined exception subclass

        Develop a user defined exception subclass for complimenting the employee when whose 
       salary is positive

     //PsalException.java

     Package javajavax.lang;
     Public class PsalException extends RuntimeException {
     Public PsalException(String s) {
      Super(s);
       }
   } 

   Compile the above packages
   
           C:\> javac –d . NsalException.java
           C:\> javac –d . PsalException.java

    2) Development of User defined General Classes:
           User defined general class makes use of user defined Exception sub class

// write a java program for developing user defined general class for checking 
all the salary of all employees of all organization.

//Emp.java
package org.igate;

import javajavax.lang.NsalException;
import javajavax.lang.PsalException;

public class Emp{
publicvoid decidesal(String s)throws NsalException,PsalException,
NumberFormatException
{
     int sal=Integer.parseInt(s);
     if(sal<=0){
      NsalException no=new NsalException(“Invalid Salary”);
       }
      else
     {
      PsalException po=new PsalException(“Valid Salary”);
      Return(po);
     }
  }
 }
       }

       Compile the above Package as
       C:\> javac –d .Emp.java
    Here org.igate.Emp is comes under defined general / common class. We can be used by 
   many employees to check their salary.

   3) Development of User defined Specified Class:

        This development is used by individual programmer and this class makes use of user 
        defined general classes and user defined Exception sub classes
  
         //Write a java program which will develop user defined specific class
        //(XEmp) for checking the salary whether they are valid or invalid.

//XEmp.java
import org.igate.Emp;
import javajavax.lang.NsalException;
import javajavax.lang.PsalException;
public class XEmp{
 public static void main(String args[]){
       try{
          String sal=args[0];
          Emp eo=new Emp();
eo.decidesal(sal);
 }
Catch(NsalException no){
    System.out.println(“Do not  enter negative salary”);
    }
Catch(PsalException po){
    System.out.println(“Ok try to improve yours salary”);
    }
Catch(NumberFormatException nfe){
   System.out.println(“PLZ enter numerical values”);
    }
Catch(ArrayIndexOutOfBoundsException nfe){
   System.out.println(“Enter the Employees salary from command prompt”);
    }
   Catch(Exceptione){
     System.out.println(e);
   }
  }
}

Ouput:

1)     C:\> javac XEmp.java
         C:\> java XEmp 9000
               Valid salary
               Ok try to improve yours salary //user defined exception message

2)       C:\> javac XEmp.java
          C:\> java XEmp -3
              Invalid salary
             Do not enter negative salary  // user defined exception message

3)       C:\> javac XEmp.java
          C:\> java XEmp
          Enter the Employees salary from command prompt
                                            // handling by predefined process or it is due to 
                                          // pre defined exception





No comments:

Post a Comment

LinkWithin