Does the same exception catch and push anything?

I've seen a lot of (commercial) code lately that throws and catches the 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)
    }
}

      

Compare this to:

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

      

Does someone actually do something, either in behavior or semantics or otherwise? Will this have any impact on the performance of the code (time and space) or does it help readability?

PS this question is very different from this question .

EDIT: The block catch

in the first does nothing other than re-remove the caught exception.

+3


source to share


3 answers


This is a violation of the basic rule:

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



Oracle guidelines indicate that every time you throw an exception, you must provide something useful for the code that catches your exception (see page 3 of Effective Java Exceptions ). Catching an exception just to re-throw it serves no practical purpose, because there is no additional information added to it for the code above in the call chain and no information retrieved by the method itself.

+6


source


It doesn't matter if you assume you provided the code. Duplicate exceptions make sense if you want to handle the exception in some way (for example, log it), but want it to be processed further in the stack trace.



As far as commercial code is concerned, perhaps the reason you saw it was because some kind of handling logic was originally handled, but the exception removal was not removed. I have seen situations like this several times.

+5


source


No reason for try/catch

in the code you posted. As with @ nickolay.laptev's answer , I believe that if you find this in commercial code, it was probably left out of the way of something that made sense.

Besides some partial error handling, there is another use case for simply finding and re-throwing an exception. This should have prevented the exclusion from being excluded from the later more general proposal catch

. For example, if you want to handle all non-instance exceptions AnException

, you either need to do type checking:

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 is cleaner and more readable in my opinion):

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

      

Then, if you later decide not to handle all of these other exceptions, you should remove the second sentence catch

and end up with the code you posted.

+1


source







All Articles