[英]Why can't I catch the exception more than one time?
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
}
}
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:
但请记住,只有一个将被执行:
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
. NumberFormatException
是RuntimeException
的子类型。当您捕获RuntimeException
,它将捕获所有类型的RuntimeException
包括NumberFormatException
。 That's why it's says already caught. 这就是为什么它说已经被抓住。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.