×
☰ See All Chapters

How to create custom exception in Java

If you are creating your own Exception then that is known as custom exception or user-defined exception.

How to create user defined custom exception?

Following are the steps to create custom exception.

  1. The class must extend any exception class. 

  2. If you want to create a checked exception extend class Exception. 

  3. If you want to create an unchecked exception extend class RuntimeException. 

Example:

class InvalidAgeException extends Exception{  

 InvalidAgeException(String s){  

  super(s);  

 }  

}

 


All Chapters
Author