Share on Google+Share on Google+

Making Custom (User Defined) Exceptions

Learn how to Making Custom (User Defined) Exceptions.

Making Custom (User Defined) Exceptions


<<<<<<< making_custom.shtml

Making Custom (User Define Exceptions)

So far you would have been known, how to be handled the exceptions in java =======

So far you would have been known, how to be handled the exceptions in java >>>>>>> 1.3 that are thrown by the Java API but sometimes you may occasionally need to throw your own exception i.e. if you encounter a situation where none of those exception describe  your exception accurately or if you can't find the appropriate exception in the Java API, you can code a class that defines an exception that is more appropriate and that mechanism of handling exception is called Custom or User Defined Exception

In Java API all exception classes have two type of constructor. First is called default constructor  that doesn't accept any arguments. Another constructor accepts a string argument that provides the additional information about the exception. So in that way  the Custom exception behaves like the rest of the exception classes in Java API.

There are two primary use cases for a custom exception. 

  •  your code can simply throw the custom exception when something goes wrong. 
  • You can wrap an exception that  provides extra information by adding your own message.

The code of a Custom exception:

public  class ExceptionClassName extends Exception


   public ExceptionClassName(){ }

  public ExceptionClassName(StringMessage)






Lets see an example that has the implementation of User Define Exception:


import java.util.*;

class MyException extends Exception
   private String nm="";
  public String getMessage(String s)

   return ("you are not permitted to enter inside "+nm);

public class ExcepDemo
  public static void main(String args[])throws MyException,IOException

  String temp="";

   String str="amit";
   System.out.println("Enter the your name");
  BufferedReader br=new BufferedReader(new InputStreamReader(;

  throw new MyException();
   System.out.println("Welcome to Rose India");
   catch(MyException e)
  catch(Exception e){


Output of the program:


C:\Roseindia\>java ExcepDemo

Enter the your name


you are not permitted to enter inside nisha

C:\Roseindia\>java ExcepDemo

Enter the your name


Welcome to Rose India

In this example we have created own exception class as MyException that throws an exception and a function with argument as getMessage that shows an exception message, if the user tries to enter the another name which doesn't match with a particular predefined name. After throwing  exception the control will be transferred  in the catch block to handle the exception where the function is  invoked to display the message included in that function.

Download this example


Posted on: April 3, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Making Custom (User Defined) Exceptions  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image