krismath
krismath

Reputation: 1969

Does catching and rethrowing the exact same exception mean anything?

Lately, I have been seeing a lot of (commercial) code that throws and catches the exact same exception, similar to the code below:

public class Foo {
    public void bar() throws AnException {
        // do something (1)
        try {
            // do something (2) -- able to throw AnException
        } catch (AnException ex) {
            throw ex;
        }
        // do something (3)
    }
}

Contrast this to:

public class Foo {
    public void bar() throws AnException {
        // do something (1)
        // do something (2) -- able to throw AnException
        // do something (3)
    }
}

Does the former actually accomplish anything, in either behavior or semantics or else? Will it have any effect on code performance (time and space) or does it help readability?

P.S. this question is very different from this question.

EDIT: The catch block in the former doesn't do anything else other than rethrowing the caught exception.

Upvotes: 5

Views: 9479

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

This is a violation of a basic rule:

The code should catch only the exceptions that it knows how to handle.

Oracle's guidelines suggests that each time you throw an exception, you should supply something useful for the code that catches your exception (see page 3 of Effective Java Exceptions). Catching an exception simply to re-throw it serves no practical purpose, because there is no additional information added to it for the code above it in the invocation chain, and no information extracted by the method itself.

Upvotes: 9

Ted Hopp
Ted Hopp

Reputation: 234857

There's no reason for the try/catch in the code you posted. As with @nickolay.laptev's answer, I believe that if you find this in commercial code, it's probably left over cruft from something that used to be meaningful.

Aside from some sort of partial error handling, there's another use case for simply catching and rethrowing an exception. This would be to prevent the exception from being caught by a subsequent, more general, catch clause. So, for instance, if you wanted to handle every exception other than instances of AnException, you would either have to do a type check:

try {
    // stuff
} catch (Exception ex) {
    if (ex instanceof AnException) {
        throw ex; // rethrow since we're not handling it
    } else {
        // handle all other exceptions
    }
}

or use something like this (which, in my view, is cleaner and more readable):

try {
    // stuff
} catch (AnException) {
    throw ex;
} catch (Exception ex) {
    // handle all other exceptions
}

Then if you later decided to not handle all those other exceptions, you'd delete the second catch clause and end up with the code you posted.

Upvotes: 1

nickolay.laptev
nickolay.laptev

Reputation: 2565

It makes no sense for sure assuming code you provided. Rethrowing exceptions makes sense when you want to handle this exception somehow (e.g. log it) but want it to be handled further in a stack trace.

Regarding commercial code, maybe the reason you saw it was that there was some handling logic initially that was removed but exceptions rethrowing wasn't removed. I saw such situations several times.

Upvotes: 7

Related Questions