Reputation: 2352
This is on Java 6
Can I have a common method to handle my exceptions - so instead of doing this n times in each method
try {
// Do something
} catch (XException e) {
// Do something
} catch (YException e) {
// Do something
} catch (ZException e) {
// Do something
}
I have
try {
// Do something
} catch (Exception e) {
handleAll (e);
}
and method handleAll(e)
does
if e.instanceOf(XException)
else if e.instanceOf(YException)
else if e.instanceOf(ZException)
Is there anything wrong with the 2nd approach?
Update:
My original question was about "centralizing the handling" in one place for both checked and runtime exceptions. The answers have pointed out I should avoid instanceof().
@aioobe's idea looks very neat to me. Are there any negative opinions on that approach?
Upvotes: 5
Views: 2627
Reputation: 6920
It's a BAD approach. It will reduce LOC (Line Of Code) but it will create difficult to understand, More resource dependent (It requires more memory and processing capacity). it also reduce Readability.
So first one is the best one
Upvotes: 2
Reputation: 39641
Java 7 will make things better. Because catching multiple exceptions is possible.
Upvotes: 1
Reputation: 421010
There is one minor problem as I see it. Since you really want the handleAll
method to rethrow any uncaught exception, it has to be declared throws Exception
. This means that so does the methods that call handleAll
.
If X-
, Y-
and ZException
are all RuntimeExceptions
I see nothing wrong with this. (I may have overlooked something though, as this is the first time I've seen this approach.)
To be sure that the instanceof
approach behaves exactly as the catch clauses would, I would consider designing the handleAll(RuntimeException e)
like this though:
private void handleAll(RuntimeException e) {
try {
throw e;
} catch (XException xe) {
...
} catch (YException xe) {
...
} catch (ZException xe) {
...
}
}
Upvotes: 4
Reputation: 234
I think every exception is unique (when compararized place in code, not the time when exception is thrown) so you should not generilaze exception handling.
You might want to handle exception little difference each time IE. if some where is thrown FileNotFoundException you might create a new file but other time that might be fatal Exception that causes applications termination.
Upvotes: 0
Reputation: 10780
The second approach will catch all Exception
s, including RunTimeException
s. Make sure you handle them correctly.
Upvotes: 0
Reputation: 691755
There is something really wrong with the second approach: if the called method signature is modified and throws a new kind of exception, the code will still compile fine, whereas you have not handled this new exception correctly.
Using the first approach, the compiler will produce an error, which will force you to handle the new exception.
Upvotes: 0
Reputation: 8245
You can do this, but I don't think it's good coding style. It's convenient to keep exception handlers close to the lines that throw the Exceptions. Suppose your code changes, and throws a new Exception. Then you have to update the method that handles them; so now you have to make changes in two places. Same happens if a particular Exception is no longer thrown by the code, or if you decide that some Exceptions should be handled at a higher level.
Also, I'm wary of "catch (Exception exc)". It's too general, try to make your Exception handlers as specific as possible.
Upvotes: 1