Custom exception in java

Custom exception:

You can define your own exception also. These exceptions are known as custom exceptions.
1. For writing custom checked exception, extend Exception class.
2. For writing custom unchecked exception, extend RuntimeException class.


 * This program is used to create custom exception.
 * @author w3spoint
class MyException extends Exception {
    public MyException(String message) {
class Test{
	public void display() throws MyException {
	  throw new MyException("This is a custom exception.");
public class CustomExceptionExample {
	public static void main(String args[]){
		//creating Test object.
		Test obj = new Test();
		//method call.
		}catch(Exception e){

Output: This is a custom exception.

Download this example.
Next Topic: Commonly used exception methods of Throwable class in java.
Previous Topic: Exception handling with method overriding in java.

Please Share