Java user defined or custom exception example

Example of user defined exception or custom exception in Java :

In java, we have different types of exceptions already available. For example, NullPointerException, ArrayIndexOutOfBoundException , ClassNotFoundException , FileNotFoundException etc. All of these exceptions invokes for specific predefine rules. For example, NullPointerException occurs if we try to do any operation on a null value, ArrayIndexOutOfBoundException occurs if we are trying to access an invalid index of a array etc.

These exceptions are enough for a normal Java program. But if we want our program to throw exceptions with different custom properties, then we will have to create separate class files for our own exception. These types of exceptions are called custom exception.

Custom exception or user defined exceptions are exceptions defined by the user. They behave same as like other exceptions i.e. we can use _try, catch, finally, throw _ etc. with these exceptions. In this tutorial, we will learn how to create custom exceptions with different examples.

Example of a custom exception :

Custom exceptions are actually subclass of Exception class. So, we need to extend a class to Exception to create a custom exception.

For example :

class CustomException extends Exception {
    String message;

    CustomException(String str) {
        message = str;

    public String toString() {
        return ("Custom Exception Occurred: " + message);

class Main {
    public static void main(String args[]) {
        try {
            throw new CustomException("This is a custom message");
        } catch (CustomException e) {

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. CustomException class is the custom exception class. Note that this class is extending Exception class.

  2. Create one local variable message to store the exception message locally in the class object.

  3. We are passing the string to the constructor of the custom exception object. The constructor set the argument string to the private string message.

  4. toString() method is used to print out the exception message.

  5. This is the main class . We are simply throwing a CustomException using one try-catch block. Notice how the string is passed while creating a custom exception. Inside the catch block, we are printing out the message.

Output :

Custom Exception Occurred: This is a custom message

java custom exception

Note that these types of exceptions are also known as checked exception.

Custom RuntimeException :

Similar to Exception or checked exception, we can also create Run-time exceptions or unchecked exceptions. Example :

class CustomRuntimeException extends RuntimeException {
    String message;

    CustomRuntimeException(String str) {
        message = str;

    public String toString() {
        return ("Custom Runtime Exception Occurred: " + message);

class Example {
    public static void main(String args[]) {
        String val = "Hello World";

        if (val.length() > 3) {
            throw new CustomRuntimeException("Length is exceeding length 3");

It will print the below output :

Exception in thread "main" Custom Runtime Exception Occurred: Length is exceeding length 3
        at Example.main(

java custom exception example

Conclusion :

We have seen in this example how to create custom exceptions in Java. We have learnt how to create a checked exception and unchcked exception with examples. Try to run these examples and if you have any queries, drop one comment below.

Similar tutorials :