views:

149

answers:

5

Are these code statements equivalent? Is there any difference between them?

private void calculateArea() throws Exception {
        ....do something
    }

private void calculateArea() {
        try {
            ....do something

        } catch (Exception e) {
            showException(e);
        }
    }
+11  A: 

Yes, there's a huge difference - the latter swallows the exception (showing it, admittedly), whereas the first one will let it propagate. (I'm assuming that showException doesn't rethrow it.)

So if you call the first method and "do something" fails, then the caller will have to handle the exception. If you call the second method and "do something" fails, then the caller won't see an exception at all... which generally a bad thing, unless showException has genuinely handled the exception, fixed whatever was wrong, and generally made sure that calculateArea has achieved its purpose.

You be able to tell this, because you can't call the first method without either catching Exception yourself or declaring that your method might throw it too.

Jon Skeet
When you mention that "Unless it genuinely handled the exception", that's a great point. I just thought I'd add that catching "Exception" itself rarely leads to intelligent "handling" of the actual exception which is the reason people recommend you catch the most specific exception possible.
Bill K
+2  A: 

Yes. The version which declares throws Exception will require the calling code to handle the exception, while the version which explicitly handles it will not.

i.e., simply:

performCalculation();

vs. moving the burden of handling the exception to the caller:

try {
    performCalculation();
catch (Exception e) {
    // handle exception
}
Lyle
+1  A: 

First one throws Exception, so the caller needs to handle the Exception. Second one catches and handles Exception internally, so the caller doesn't have to do any exception handling.

Samit G.
So in a nutshell , I should always use the second one. Am I right?The first one is actually a method which is used in different point of the program. That's why I decided to gruop together the instructions for further use but having done that I now realize that T was making a big mistake..
carlos
No, both patterns are needed. If your method can handle the exception, use the second pattern, if not, use the first one to notifiy the caller.
Andreas_D
Which version you use depends on your requirements - basically at what level do you need to handle that exception. The caller needs to be coded accordingly. If a caller was calling the first version, and you replace the method definition with the second version, your caller code will be forced to handle the exception as this is a checked exception.
Samit G.
A: 

Yes, there is a great deal of difference between them. The in the first code block, you pass the exception to the calling code. In the second code block you handle it yourself. Which method is correct depends entirely on what you are doing. In some instances, you want your code to handle the exception (if a file isn't found and you want to create it, for instance) but in others, you want the calling code to handle the exception (a file isn't found and they need to specify a new one or create it).

Generally speaking as well, you don't want to catch a generic exception. Instead, you'll want to catch only specific ones, such as FileNotFoundException or IOException because they can mean different things.

Chris Thompson
+1  A: 

I assume that by "identical" you are referring to behavior.

A behavior of a function can be determined by:

1) Returned value

2) Thrown exceptions

3) Side effects (i.e changes in the heap, file system etc)

In this case, the first method propagates any exception, while the second throws no checked exception, and swallows most of the unchecked exceptions as well, so the behavior IS different.

However, if you guarantee that "do something" never throws an exception, then the behavior would be identical (though the compiler will require the caller to handle the exception, in the first version)

--edit--

From the point of view of API design, the methods are completely different in their contract. Also, throwing class Exception is not recommended. Try throwing something more specific to allow the caller to handle the exception better.

Eyal Schneider