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

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

问题描述

如果我没有错误,应该首先抓住例外的子类。但是必须先捕获任何RuntimeException和一个具体的检查的Exception,这个应该在最初被捕获?

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

这个订单是否正确?或者是正确的,但是一个坏的选择?

解决方案

顺序是无论什么匹配,执行因为JLS清楚地解释了)。



如果第一个catch匹配异常,它将执行,如果没有,则下一个被尝试并打开,直到一个匹配或无所以,当捕获异常时,您希望始终抓住最强大的,然后最通用(如RuntimeException或异常)。例如,假设你想抓住 StringIndexOutOfBoundsException %29rel =noreferrer> String.charAt(index)方法,但你的代码也可以抛出一个 NullPointerException ,以下是如何捕获例外:

  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 ,则它们不会跳过,否则它将进入第一个catch,如果 StringIndexOutOfBoundsException 进入第二个,最后如果它是另一个RuntimeException (或从它继承,如 IllegalArgumentException )它进入第三个catch



您的情况是正确的,因为IOException继承自Exception和RuntimeException继承自Exception,所以他们不会彼此跳过。



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

  try {
// some code here
} catch(Exception e){
e.printStackTrace();
} catch(RuntimeException e){//这行将导致编译错误,因为它将永远不会被执行,因为第一个catch会选择异常
e.printStackTrace();
}

所以,你应该先让孩子,然后是父例外。 p>

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?

解决方案

The order is whatever matches first, gets executed (as the JLS clearly explains).

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.

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();
}

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.

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天全站免登陆