简体   繁体   English

为什么我不能多次捕获异常?

[英]Why can't I catch the exception more than one time?

JLS 8, 14.20 : JLS 8,14.20

A try statement executes a block. try语句执行一个块。 If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause . 如果抛出一个值,并且try语句具有一个或多个catch子句可以捕获它,则控制权将转移到第一个此类catch子句

It looks like I may have for instance two catch clauses for the same exception type. 好像我可能有两个catch子句用于同一个异常类型。 But when I try this I'll receive compile-time error. 但是当我尝试这样做时,我会收到编译时错误。

public static void main (String[] args) throws java.lang.Exception
{
    try{
    } catch(RuntimeException ioe){
    } catch(NumberFormatException e){ //Already caught
    } 
}

IDEONE 爱迪生

Could you explain it using JLS? 您能用JLS解释一下吗?

NumberFormatException is a specialization of RuntimeException, so your NumberFormatException would be already caught by the first statement. NumberFormatException是RuntimeException的一种特殊形式,因此您的NumberFormatException将已经被第一条语句捕获。 You can switch the order of those two catch clauses, however; 但是,您可以切换这两个catch子句的顺序。 but keep in mind only one will be executed: 但请记住,只有一个将被执行:

  • the NumberFormatException clause if the exception has this type NumberFormatException子句(如果异常具有此类型)
  • the RuntimeException clause for all other types of RuntimeException 所有其他类型的RuntimeException的RuntimeException子句

As to why it is this way... Well that is how the language was designed. 至于为什么用这种方式……那么这就是语言的设计方式。 If all the matching catch blocks were executed, it would be really harder to handle errors correctly. 如果所有匹配的catch块都已执行,那么正确地处理错误将变得更加困难。

Because RunTimeException is base class of NumberFormatException. 因为RunTimeException是NumberFormatException的基类。 http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html

The compiler error you get results from the hierarchy of the catched exceptions. 您从捕获的异常的层次结构中获得结果的编译器错误。

NumberFormatException extends IllegalArgumentException extends RuntimeException

Think of the catch block as safety nets . 将捕获块视为安全网 The NumberFormatException net is quite small as it is a special case of higher hierarchy exceptions. NumberFormatException网络非常小,因为它是更高层次结构异常的特例。 The RuntimeException net is one of the largest possible and will catch anything on this level (except Exception and Throwables, which are "superior"). RuntimeException网络是可能最大网络之一,它将捕获此级别上的所有内容(“异常”和“异常”除外)。 What you did is place the small net below the large net . 您所做的是将小网放置在大网下方 So the compiler is polite enough to give you the hint that the smaller net will never be reached. 因此,编译器很有礼貌,可以提示您永远不会达到较小的网络。

(I know the metapher is not 100% precise, but in this context works fine.) (我知道metapher不是100%精确的,但是在这种情况下可以正常工作。)

If you catch NumberFormatException first and then RuntimeException the compiler will happily agree! 如果先捕获NumberFormatException,然后再捕获RuntimeException,则编译器将很高兴达成共识! :-) You can also rethrow your caught exception from the first block. :-)您也可以从第一个块中抛出捕获的异常。

NumberFormatException is a sub type of RuntimeException When you caught RuntimeException , It will caught all type of RuntimeException including NumberFormatException . NumberFormatExceptionRuntimeException的子类型。当您捕获RuntimeException ,它将捕获所有类型的RuntimeException包括NumberFormatException That's why it's says already caught. 这就是为什么它说已经被抓住。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能多次捕获异常 - cannot catch the exception more than one time 为什么我不能添加多个 Object 到 JPanel? - Why I can't add more than one Object to the JPanel? 为什么不能用try / catch子句处理Exception e? - Why can't I handle Exception e with try / catch clause? 为什么我不能使用多个Java递增或递减运算符? - Why do I can't use more than one Java increment or decrement operators? 为什么我不能自动连接AuthorizationServerTokenServices或ConsumerTokenServices? “这个类型的豆子不止一个” - Why can't I auto-wire AuthorizationServerTokenServices or ConsumerTokenServices? “There is more than one bean of [this] type” 为什么我不能在多个 controller 中使用 JPA 存储库? - Why can't I use a JPA repository in more than one controller? 如何在javafx 8中多次使用控件? - How can i use a control more than one time in javafx 8? 如何同时滚动多个对象? - How can I scroll more than one object at the same time? 我不明白为什么我的一个客户端程序不能向服务器发送多于一条消息? - I don't understand why one of my client program can't send more than one message to the server? 为什么我可以创建多个具有反射的单例实例? - Why can I create more than one instance of a singleton with reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM