In this tutorial, we will learn about Custom Exception in Java. Custom Exceptions are also known as a user-defined exception. The main motive of creating a custom exception is to create a new exception according to project requirement. When you create a custom exception you have to inherit Exception class as a parent in your custom exception class. please check out the following example.
Page Contents
Example Program:
//creating an custom Exception class
class MyCustomException extends Exception{
public MyCustomException(){
System.out.println("Constucor of MyCustomException invoked");
}
public String getMessage() {
return "Hello, This is getMessage() in MyCustomException";
}
}
public class Example{
public static void main(String args[]){
try{
System.out.println("In try block");
throw new MyCustomException(); //throwing exception explicitly
}catch(MyCustomException e){
System.out.println(e.getMessage());
System.out.println("In catch block");
}
}
}
Output:
In try block
Constucor of MyCustomException invoked
Hello, This is getMessage() in MyCustomException
In catch block

Parvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​