为什么命名的例外被选中和取消选中? [英] Why are exceptions named checked and unchecked?
问题描述
为什么Java中的两个异常类型名为checked和unchecked?这些名称背后的原因是什么?
Why is the two exception types in Java named "checked" and "unchecked"? What is the reason behind those names?
推荐答案
如果你调用一个声明抛出检查的方法异常(例如 IOException
),编译器将检查您要么正在捕获它,要么声明您要重新抛出它。同样,为了首先抛出这样一个检查过的异常,编译器检查你已经将它声明为方法签名的一部分。
If you call a method which is declared to throw a checked exception (such as IOException
), the compiler will check that you're either catching it or declaring that you rethrow it. Likewise, in order to throw such a checked exception in the first place, the compiler checks that you've declared it as part of the method signature.
基本上,它有点像类型检查,除了可以通过方法抛出的异常。
Basically, it's a little bit like type checking, except in terms of which exceptions can be thrown by a method.
编译器不执行任何检查未经检查的异常 - 因此可以通过任何方法抛出它们,而不会使用声明它们的方法。
The compiler doesn't perform any checking for unchecked exceptions - so they can be thrown by any method, without the method declaring them.
这篇关于为什么命名的例外被选中和取消选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!