views:

160

answers:

5

In Java, I want to do something like this:

try {
    ...     
} catch (IllegalArgumentException, SecurityException, 
       IllegalAccessException, NoSuchFieldException e) {
   someCode();
}

...instead of:

try {
    ...     
} catch (IllegalArgumentException e) {
    someCode();
} catch (SecurityException e) {
    someCode();
} catch (IllegalAccessException e) {
    someCode();
} catch (NoSuchFieldException e) {
    someCode();
}

Is there any way to do this?

+7  A: 

No, one per customer.

You can catch a superclass, like java.lang.Exception, as long as you take the same action in all cases.

But that might not be the best practice. You should only catch an exception when you have a strategy for actually handling it - and logging and rethrowing is not "handling it". If you don't have a corrective action, best to add it to the method signature and let it bubble up to someone that can handle the situation.

duffymo
Can I petition you to rephrase the portion about catching java.lang.Exception? I realize that it's an example, but I feel like some people might read this answer and say, "oh, okay, I'll just catch Exception then", when that's probably not what they want to (or should) do.
Rob Hruska
See if that's better.
duffymo
I knew about that, but I don't want to do it... Oh, well, guess I'm stuck with 4 catches then, till the next version of Java...
froadie
Yep, thanks for the update.
Rob Hruska
@duffymo: What's wrong with logging and rethrowing? Except that it clutters the code, its equivalent to not catching it, isn't it. Seen from the general error-handling strategy perspective. What's bad is logging and _not_ rethrowing.
Frank
I don't consider logging and rethrowing handling anything. I'd prefer to let it bubble up to someone who can do something meaningful. That last layer where exceptions should never escape (e.g. controllers in a web app) should be the one to log the error in that case.
duffymo
A: 

Catch the exception that happens to be a parent class in the exception hierarchy. This is of course, bad practice. In your case, the common parent exception happens to be the Exception class, and catching any exception that is an instance of Exception, is indeed bad practice - exceptions like NullPointerException are usually programming errors and should usually be resolved by checking for null values.

Vineet Reynolds
+5  A: 

No.

That is proposed for the next version of Java, but currently that's not possible.

If all the exceptions belong to the same class hierarchy, you can catch that, otherwise you'll have to catch them all.

OscarRyz
That's annoying :-/ Talk about code duplication...
froadie
+2  A: 

If there is a hierarchy of exceptions you can use the base class to catch all subclasses of exceptions. In the degenerate case you can catch all Java exceptions with:

try {
   ...
} catch (Exception e) {
   someCode();
}

In a more common case if RepositoryException is the the base class and PathNotFoundException is a derived class then:

try {
   ...
} catch (RepositoryException re) {
   someCode();
} catch (Exception e) {
   someCode();
}

The above code will catch RepositoryException and PathNotFoundException for one kind of exception handling and all other exceptions are lumped together.

Michael Shopsin
BTW catch clauses are handled in order so if you put a parent exception class before a child class then it's never called eg:try { ...} catch (Exception e) { someCode();} catch (RepositoryException re) { // never reached}
Michael Shopsin
@Michael: Actually precisely because it can never be reached, such code doesn't even compile.
polygenelubricants
+3  A: 

Within Java 7 u will be able to define multiple catch clauses like:

catch (IllegalArgumentException | SecurityException e)
{
    ...
}
ymene