User Defined Exceptions in Java

While working on an application, you may come across some scenarios where you feel like the Exceptions defined in the java platform are not enough to represent the error/issue you want to tell the end user. In that case you may choose to write your own exception. That is called a User Defined or Custom Exception.
Lets see how to define User Defined Exceptions in Java.

While creating a user defined exception, your exception should always extend from Exception or any of its Subclass. Also its always good to append the string Exception to the names of all classes that inherit (directly or indirectly) from the Exception class. This will help readability.

User Defined Exception class

Use the Exception in your code

Handling your exception


If a Exception is thrown then the Catch block will show the below in the console.

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn