本文翻译自:Can I catch multiple Java exceptions in the same catch clause?
In Java, I want to do something like this: 在Java中,我想做这样的事情:
try {
...
} catch (/* code to catch IllegalArgumentException, SecurityException,
IllegalAccessException, and NoSuchFieldException at the same time */) {
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? 有没有办法做到这一点?
参考:https://stackoom.com/question/EfRu/我可以在同一个catch子句中捕获多个Java异常吗
Not exactly before Java 7 but, I would do something like this: 不完全是在Java 7之前,但我会做这样的事情:
Java 6 and before Java 6和之前的版本
try {
//.....
} catch (Exception exc) {
if (exc instanceof IllegalArgumentException || exc instanceof SecurityException ||
exc instanceof IllegalAccessException || exc instanceof NoSuchFieldException ) {
someCode();
} else if (exc instanceof RuntimeException) {
throw (RuntimeException) exc;
} else {
throw new RuntimeException(exc);
}
}
Java 7 Java 7
try {
//.....
} catch ( IllegalArgumentException | SecurityException |
IllegalAccessException |NoSuchFieldException exc) {
someCode();
}
A cleaner (but less verbose, and perhaps not as preferred) alternative to user454322's answer on Java 6 (ie, Android) would be to catch all Exception
s and re-throw RuntimeException
s. 用户454322对Java 6(即Android)的回答更清晰(但不那么冗长,也许不是首选)将是捕获所有Exception
并重新抛出RuntimeException
。 This wouldn't work if you're planning on catching other types of exceptions further up the stack (unless you also re-throw them), but will effectively catch all checked exceptions. 如果您计划在堆栈中进一步捕获其他类型的异常(除非您也重新抛出它们),这将无法工作,但会有效地捕获所有已检查的异常。
For instance: 例如:
try {
// CODE THAT THROWS EXCEPTION
} catch (Exception e) {
if (e instanceof RuntimeException) {
// this exception was not expected, so re-throw it
throw e;
} else {
// YOUR CODE FOR ALL CHECKED EXCEPTIONS
}
}
That being said, for verbosity, it might be best to set a boolean or some other variable and based on that execute some code after the try-catch block. 话虽如此,为了详细说明,最好设置一个布尔值或其他变量,并根据它在try-catch块之后执行一些代码。
In pre-7 how about: 在7之前如何:
Boolean caught = true;
Exception e;
try {
...
caught = false;
} catch (TransformerException te) {
e = te;
} catch (SocketException se) {
e = se;
} catch (IOException ie) {
e = ie;
}
if (caught) {
someCode(); // You can reference Exception e here.
}
No, one per customer. 不,每个客户一个。
You can catch a superclass, like java.lang.Exception, as long as you take the same action in all cases. 只要在所有情况下采取相同的操作,您就可以捕获超类,例如java.lang.Exception。
try {
// some code
} catch(Exception e) { //All exceptions are caught here as all are inheriting java.lang.Exception
e.printStackTrace();
}
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, better to add it to the method signature and let it bubble up to someone that can handle the situation. 如果您没有采取纠正措施,最好将其添加到方法签名中,然后让它冒泡到可以处理这种情况的人。
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. 在您的情况下,公共父异常恰好是Exception类,并且捕获任何异常实例的异常,确实是不好的做法 - 像NullPointerException这样的异常通常是编程错误,通常应该通过检查空值来解决。