Java – User Defined Exceptions


The other way of classifying exceptions is by its definition

  • Built-in Exceptions
  • User-defined Exceptions
  • Built-in Exceptions are those which are already available under the Exception class of java.
  • User-defined Exceptions are defined by the user/programmer.

This can be done by creating a user defined class extending the Exception class


Class <MyExceptionClass> extends Exception




User can throw the exception he created by using throw keyword.


Syntax 1:

<MyExceptionClass> obj = new MyExceptionClass(“arguments”);

throw obj;

Syntax 2:

throw new MyExceptionClass();

Example for user defined exception:

Best Practices in Error Handling:

  • Make use of improved features (Introduced from Java SE 7)
  • Improved catch block.

Sometimes, we have to write the same code inside multiple catch blocks.


Now the handling of all the exceptions are the same in this case. Java 7 has introduced an improved catch block which can combine the multiple exceptions which has the same handling code.

Same code with the enhanced catch block

This makes the code to remain simple.

Auto Resource Management in Java SE 7:

Previously we need to write the finally block to do the clean up work. In Java SE 7, there is a feature to define the resources in the try block and as soon as the try catch block finishes executing, the resources are automatically closed. A separate finally block is not needed to perform this task


The same code can be written without the finally block, since the resource needed are defined in the try block like arguments. When the try block finishes executing, resources defined like arguments inside parenthesis will be automatically closed in Java SE 7.

Rethrowing an exception:

We may need to rethrow an exception again sometimes.

Consider the following example

In the above example, method1 is invoked which tends to throw an exception. Now it is caught in the catch block of method1(). Again inside catch block Runtime exception is thrown, hence the method method1() now throws an exception to the main method which has an catch block inside it to handle the rethrown exception.

But here the 1st exception thrown is wrapped into the runtime exception. The exception which was thrown in method1() was not the same which was again rethrown. To rethrow the exact exception again, we need to catch it as a general exception in method1 and throw it. The catch block in the main method would be now catching the specific exception.

This would rethrow the same exception which was caught.

Do not avoid or ignore an exception:

  • Adding an empty catch block after a try block and leaving the system to handle is not a good practice of exception handling.
  • API can identify checked exceptions and intimate the same so that programmer can catch them as needed. But unchecked exceptions need to be handled by analyzing the logic of code present in try block or by adding generic exception catch block
  • If the programmer is not able to do anything with a checked exception he could throw a runtime exception instead of adding an empty catch block or just not handling at all.
  • Unless it is very much important to handle an runtime exception, do not handle it. Not every exception has to be specified and handled in the catch block.
  • Either log the exceptions or throw them. Do not perform both. This may confuse the person who is analyzing all the exceptions.
  • Do not catch top level exceptions in general until a lower level exception cannot be found suitable for the try block logic.

  • Unchecked exceptions are in the class of runtime exceptions which in turn are under the class of exceptions. So while catching Exception class, we are also including all the subclasses of it.


Like it? Please Spread the word!