Java Rethrow Exception Tutorial

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

What does it mean to Rethrow an Excepetin in Java?

An exception that is caught can be rethrown. You may want to rethrow an exception for different reasons. One of the reasons could be to take an action after catching it, but before propagating it up the call stack.

For example, you may want to log the details about the exception and then rethrow it to the client. Another reason is to hide the exception type/location from the client. You are not hiding the exceptional condition itself from the client. Rather, you are hiding the type of the exceptional condition.

You may want to hide the actual exception type from clients for two reasons:

  • The client may not be ready to handle the exception that is thrown.
  • The exception that is thrown does not make sense to the client.

Rethrowing an exception is as simple as using a `throw` statement.

When the same exception object is rethrown, it preserves the details of the original exception.

Example: rethrowing an exception in Java

public class Simple {

    public static void main(String[] args) {
        try{
            check();
        }catch (Exception e){
            System.out.println("In the body of the catch block in main method.");
            System.out.println(e.getMessage());
        }
    }
    public static void check(){
        try{
            int res = 30/0;
        }catch (ArithmeticException exception){
            store(exception);
            throw exception;
        }
    }
    public static void store(Exception exception){
        try(FileWriter writer = new FileWriter("G:/error.txt",true)){
            for (StackTraceElement element : exception.getStackTrace()){
                writer.write("Message: "+exception.getMessage()+"\n");
                writer.write("The file name is: "+element.getFileName()+"\n");
                writer.write("The class name is: "+element.getClassName()+"\n");
                writer.write("The method name is: "+element.getMethodName()+"\n");
                writer.write("The line number is: "+element.getLineNumber()+"\n");
                writer.write("************************\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

In the body of the catch block in main method.

/ by zero

Java rethrow exception note:

When an exception is thrown from a catch block, another catch block in the same group is not searched to handle that exception. If you want to handle the exception thrown from a catch block, you need to enclose the code that throws the exception inside another try-catch block. Another way to handle it is to enclose the whole try-catch block inside another try-catch block.

In this example, we’ve called the `check()` method inside the body of the `main()`.

In the `check()` method, and exception occurred and we handle it via the `try catch` block here in the method.

In the `catch` block we’ve called the `store()` method to save the details of the exception into a file and next we re-throw that exception. So now, the `main()` method is also responsible to handle the exception as well.

As you can see, in the `main()` method, the `catch` block also ran and we saw its message on the output stream.

Note: The FileWrite class is explained in I/O section of the tutorial. So for now, just remember that via this class we can create objects and use them to store string characters in a file.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies