User defined exception in Java (Custom exception) Tutorial

In this section, we will learn what the user defined exception is and how it works in Java.

What is user defined exception in java? (Java Custom Exception)

In Java, other than those built-in exception classes that exist, we can build our own exception class as well. This is known as custom exception and in this section we will learn how to create them.

How to create user defined exception in Java?

To create a user defined exception (AKA custom exception) in Java, we need to extend a class to inherit from a built-in exception class.

For example, classes like `Throwable` and `Exception` are examples of classes that can be used for this purpose.

Example: creating a custom exception in Java

Create a class named `MyException` and put the code below into that file:

public class MyException extends Exception {

    public MyException(String message){
        super(message);
    }
}

As you can see, the `MyException` class inherited from the `Exception` class. Inside this class, we’ve created a constructor that takes one string argument and sends this message to the constructor of the `Exception` class as well so that it can use this message to initialize one of its members.

Now this class has the access to the public members of the `Exception` class. For example, one of the public methods is `getMessage()` in which calling the method will return the message that we set as the argument to the constructor when we were building an object of this class.

Alright, now create a class named `Simple` and put the code below into that file:

public class Simple {

    public static void main(String[] args) {
        try{
            throw new MyException("Simple exception");
        }catch (MyException e){
            System.out.println(e.getMessage());
        }
    }
}

Output: Simple exception

In this example, we’ve created an object of type `MyException` and because this class inherits from the `Exception` class, we can use the `throw` keyword to throw this object (exception). And of course the `catch` block can be used to handle this exception.

The `MyException` class is a subclass of the `Exception`, so we could also use the `Exception` as the type of the parameter in the `catch` block.

Java Custom Exception Note:

Typically, we do not add any methods to our custom exception class. Many useful methods that can be used to query an exception object’s state are declared in the `Throwable` class and we can use them without re-declaring them. Typically, we include four constructors to our custom exception class.

These constructors are:

public MyExceptionClass() {
}

public MyExceptionClass (String message) {
    super(message);
}

public MyExceptionClass (String message, Throwable cause) {
    super(message, cause);
}

public MyExceptionClass (Throwable cause) {
    super(cause);
}

These constructors are also set in the Throwable class. So inside each of these constructors we call the `super` keyword to call the parent constructor and initialize it as well.

So if we refactor the `MyException` class in the example above, we will have this:

public class MyException extends Exception {

    public MyException() {
    }

    public MyException(String message) {
        super(message);
    }

    public MyException(String message, Throwable cause) {
        super(message, cause);
    }

    public MyException(Throwable cause) {
        super(cause);
    }
}

 

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies