Home Java Java-exception User Defined Exception
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

User Defined Exception

Advertisement
As we come across Built -in-exception, you create own customized exception as per requirements of the application.

User Defined Exception

     

As we come across Built -in-exception, you create own customized exception as per requirements of the application. On each application there is a specific constraints. For Example, In case of Air Flight Booking, a passenger must specify the source ,destination place and age. Passenger must brings a ticket who is above the age of   3 years. Similarly in the case of a banking application, A Customer whose age is less than Eighteen need to open Joint account.Thus,Error-handling become necessary while developing a constraint application The Exception class and its subclass in java is not able to meet up the required constraint in application. For this, you create your own customized Exception to over address these constraints and ensure the integrity in the application.

 

 

How to Handle and create User-defined Exception

The keywords used in java application are try, catch and finally are used in implementing used-defined exceptions. This Exception class inherits all the method from Throwable class.

List of Method defined by Throwable  Class

   Methods    Explanation 
String toString( ) Gives you a String object and description of the exception. This  methods is called by the println ( ) method when an object of throwable is passed to it as argument.
String getMessage( ) Gives you the description of the exception in program
Throwable fillInStackTrace( ) Gives you aThrowable Object that contains a stack trace.
void print StackTrace( ) Gives you and print the stack trace
void printStackTrace(PrintStream stream) Return the stack trace to a specific defined stream
String getLocalizedMessage Return the Localized description of the exception


Understand User-Defined Exceptions with Example-

In the given below code, the UseDefinedException,subclass of  the Exception class is created. The UseDefinedException class has one constructor,i.e UseDefinedException( ).

The String toString( ) method returns you string containing the description of the exception.The Test Class defines the method mm that throwan object of UseDefinedException,The Exception is thrown when marks is less than 50.

Creating a UserDefined Exception

class UseDefinedException extends Exception{
   String msg = "";
  int marks;
   public UseDefinedException() ){

 }
   public UseDefinedException(String str){
   super(str);
 }
  public String toString(){
   if marks <= 50)
   msg = "You have failed";  

  if marks > 50) 

   msg = "You have Passed";
   return msg;
}
}
  public class test{
  public static void main(String args[]){
  test t = new test();
   t.mm();
}
  public void mm(){
  try{
   int i=0;

  if( i < 50) 
  throw new MyException();

}
   catch(MyException ee1){
  System.out.println("my ex"+ee1);
}
}
}

Output on the Command Prompt

C:\saurabh>javac test.java

C:\saurabh>java test
my exYou have failed
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 10, 2008

Related Tutorials

Ask Questions?    Discuss: User Defined Exception   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com