Java中捕获异常的顺序 [英] Order of catching exceptions in Java

查看:17
本文介绍了Java中捕获异常的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没记错的话,应该首先捕获 Exceptions 的子类.但是必须捕获任何 RuntimeException 和一个具体的检查异常,应该首先捕获哪个?

If I'm not mistaken, subclasses of Exceptions should be caught first. But having to catch any RuntimeException and a concrete checked Exception, which should be caught at first?

try {
    ...
} catch(RuntimeException e) {
    ...
} catch(IOException e) {
    ...
}

这个顺序正确吗?或者它是正确的,但是一个糟糕的选择?

Is this order a correct one? Or is it correct but a bad option?

推荐答案

顺序是先匹配的,先执行 (JLS 清楚地解释了).

如果第一个 catch 与异常匹配,则执行,如果不匹配,则尝试下一个,直到匹配或不匹配为止.

If the first catch matches the exception, it executes, if it doesn't, the next one is tried and on and on until one is matched or none are.

因此,在捕获异常时,您希望始终先捕获最具体的,然后再捕获最通用的(如 RuntimeException 或 Exception).例如,假设您想捕获 StringIndexOutOfBoundsExceptionString.charAt(index) 方法,但您的代码也可能抛出 NullPointerException,这里是你如何去捕捉异常:

So, when catching exceptions you want to always catch the most specific first and then the most generic (as RuntimeException or Exception). For instance, imagine you would like to catch the StringIndexOutOfBoundsException thrown by the String.charAt(index) method but your code could also throw a NullPointerException, here's how you could go to catch the exceptions:

String s = null;
try {
  s.charAt(10);
} catch ( NullPointerExeption e ) {
  System.out.println("null");
  e.printStackTrace();
} catch ( StringIndexOutOfBoundsException e ) {
  System.out.println("String index error!");
  e.printStackTrace();
} catch ( RuntimeException e ) {
  System.out.println("runtime exception!");
  e.printStackTrace();
}

所以,按照这个顺序,我确保异常被正确捕获并且它们不会相互绊倒,如果它是一个 NullPointerException 它进入第一个捕获,如果一个 StringIndexOutOfBoundsException 它进入第二个,最后如果它是 RuntimeException 的其他东西(或从它继承,如 IllegalArgumentException),它进入第三个捕获.

So, with this order, I am making sure the exceptions are caught correctly and they are not tripping over one another, if it's a NullPointerException it enters the first catch, if a StringIndexOutOfBoundsException it enters the second and finally if it is something else that is a RuntimeException (or inherits from it, like a IllegalArgumentException) it enters the third catch.

您的情况是正确的,因为 IOException 继承自 Exception,而 RuntimeException 也继承自 Exception,因此它们不会相互绊倒.

Your case is correct as IOException inherits from Exception and RuntimeException also inherits from Exception, so they will not trip over one another.

首先捕获通用异常然后再捕获它的一个后代也是一个编译错误,如:

It's also a compilation error to catch a generic exception first and then one of it's descendants later, as in:

try {
  // some code here
} catch ( Exception e) {
  e.printStackTrace();
} catch ( RuntimeException e ) { // this line will cause a compilation error because it would never be executed since the first catch would pick the exception
  e.printStackTrace();
}

所以,你应该先有孩子,然后是父母例外.

So, you should have the children first and then the parent exceptions.

这篇关于Java中捕获异常的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆