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){
  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();;
  public void mm(){
   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>java test
my exYou have failed

Share on Google+Share on Google+

User Defined Exception

Posted on: October 23, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: User Defined Exception   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 6, 2011

This is not working program... check your code once. Concept is correct. Application is wrong..
January 11, 2012
Erroneous Code

the code you mentioned for user-defined exception is wrong. * you created a Exception with the name Userdefined exception and calling with MyException, please correct the code so that is will make useful to other users.