Java Custom Exception

We can create our own exception that is called as Custom Exception or User Defined Exception. Custom exceptions are used to customize the exceptions and messages according to user needs.

Steps to create Custom Exception

Below are steps to create custom exception:

  1. Create a class extends with one of the exceptions which are sub types of the java.lang.Exception class. Generally exception class always extends directly from the Exception class.
  2. Create a constructor with String parameter which is the detailed message of the exception.
  3. In this constructor, simply call the super constructor and pass the message.

Note : There is convection to use Custom Exception name ends with word ‘Exception’.

Steps Custom Exception or User Defined Exception
Custom Exception or User Defined Exception

In below example create a Custom Exception as EmployeeNotFoundException
which is extending class java.lang.Exception and create different constructors to handle exception with exception message and stack traces.

public class EmployeeNotFoundException extends Exception{
        //Constructor to throw exception with message
	public EmployeeNotFoundException(String message)
	{
		super(message);
	}
        //Constructor to throw exception with stack trace
	public EmployeeNotFoundException(Throwable cause)
	{
		super(cause);
	}
        //Constructor throw exception meth message and stack trace
	public EmployeeNotFoundException(String message,Throwable cause)
	{
		super(message,cause);
	}
}

Example to use Custom Exception

import java.util.Arrays;
import java.util.List;
public class CustomExceptionTest {
    public static void main(String[] args) throws EmployeeNotFoundException{
    String[] empArr ={"Saurabh", "Gaurav", "Shailesh", "Ankur", "Ranjith", "Ramesh"};
    List empList = Arrays.asList(empArr);
    String searchStr = "Rishabh";
	try {
	   if (!empList.contains(searchStr)) {
//Throw exception when emmployee name not match with existing list with customize message.
	throw new EmployeeNotFoundException("Employee not found in search");
	     }
	} catch (Exception ex) {
	 throw new EmployeeNotFoundException("Employee not found",ex);
	}
	}
}

Output

Exception in thread "main" com.customexceptions.EmployeeNotFoundException: Employee not found
    at com.customexceptions.CustomExceptionTest.main(CustomExceptionTest.java:16)
Caused by: com.customexceptions.EmployeeNotFoundException: Employee not found in search
    at com.customexceptions.CustomExceptionTest.main(CustomExceptionTest.java:13)


Conclusion

In this topic , You have learn about :

  • What is Custom Exception or User-defined exception?
  • How to create Custom Exception or User-defined Exception?
  • and How to implement it?

Learn More on Java Exception Handling

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s