Reputation: 968
One of the projects I inherited is riddled with tons of try/catch blocks catching the general Exception
. I've been slowly but surely refactoring this, but there are so many, such that I have been contemplating bringing this up as a concern in a meeting. This got me to thinking...Is there ever really a case where catching the general exception is justified in a production environment? I could not think of a case where I NEEDED to catch the general exception, but I'm also a fairly recent grad and I'm sure there's tons that I don't know. I did a little research, and I can find lot's of reasons why NOT to catch the general exception, but nothing concrete on when this practice is justified. Obviously if you're calling a method that already throws Exception you have to catch it. But is there ever a reason some method could throw Exception
and it should not be refactored to throw the specific exception.?
Upvotes: 0
Views: 537
Reputation: 2821
Catching general Exception
isn't best practice, because if you are catching exception you are telling that you can handle it and recover from that exception state, but if you can't recover then it might be better to fail than to keep working with very unpredictable state.
Another thing that can happen is to catch exception that is supposed to be handled at higher level which can again lead to dangerous state.
There is possibility that code was written before Java 7 when multi-catch was introduced so they used Exception
instead of writing each separately, or that developer wasn't familiar with this.
Only case in which catching Exception
is justified, in my opinion at least, is at top of the application(main
) - catch all exceptions that are not handled at lower levels, log them and exit for safety reasons, and crash nicely and show reasonable message to end user.
This brings us to another thing, and that is throwing Exception
, same as with catching one you shouldn't throw Exception
, that is same like returning Object from every method, you lose identity.
If this two things are very common in project you are working on maybe you should consider mentioning that to senior developer.
Upvotes: 1
Reputation: 140318
Throw Exception
only if you need to throw Exception
, specifically. If you throw too-general an exception, you are effectively just shouting "there is a problem", without giving specific information as to what that problem is.
Catch Exception
only if Exception
is thrown, specifically. If you catch too general an exception, you're losing the opportunity to handle specific exceptions in the correct way.
Throwing Exception
is the equivalent of returning Object
instead of a more-specific type which would be useful to the caller; catching Exception
is the equivalent of assigning a return value to an Object
variable, rather than a more specific type that you could do useful things with. Basically: you are discarding available type information.
Sometimes you have to throw Exception
, because you are writing a general framework. For example, Callable.call
throws Exception
, because you don't know what code will be executed there, so allowing it to throw Exception
means that you don't constrain users of the class. And consequently, if you're calling a Callable
, you need to catch Exception
; but you need to do it with care.
The vast majority of people aren't (or shouldn't be) writing frameworks, and so you shouldn't be throwing or catching Exception
.
There is good advice on this in Effective Java, Item 61, " Throw exceptions appropriate to the abstraction" (this is the number in 2nd Ed; don't know about 3rd Ed). Basically: you almost certainly don't want to throw Exception
, but you might want to throw IOException
rather than FileNotFoundException
, if the fact that you're reading from a file isn't relevant to your API.
Upvotes: 2