Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
User Defined Exception 
 

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

                         

» View all related tutorials
Related Tags: c exception io stack type exec uri lock this block exe row handle program to execution ram each pos ast

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.