[英]Exception doesn't need to be thrown to be caught, but IOException does
Why does the following code compile fine, but the method being called does not need to throw Exception
? 为什么以下代码可以正常编译,但是被调用的方法不需要抛出
Exception
? Isn't Exception
a checked exception and not an unchecked exception? Exception
不是检查异常还是非检查异常吗? Please clarify. 请澄清。
class App {
public static void main(String[] args) {
try {
amethod();
System.out.println("try ");
} catch (Exception e) {
System.out.print("catch ");
} finally {
System.out.print("finally ");
}
System.out.print("out ");
}
public static void amethod() { }
}
If I want to use a try catch with IOexception
(a checked exception), the method being called needs to throw IOException
. 如果我想对
IOexception
(尝试异常)使用try catch, IOexception
调用的方法需要抛出IOException
。 I get this. 我明白了
import java.io.IOException;
class App {
public static void main(String[] args) {
try {
amethod();
System.out.println("try ");
} catch (IOException e) {
System.out.print("catch ");
} finally {
System.out.print("finally ");
}
System.out.print("out ");
}
public static void amethod() throws IOException { }
}
Isn't 'Exception' a checked exception and not an unchecked exception?
“异常”不是已检查的异常,还是未检查的异常吗?
Yes, it is. 是的。
But even if we know the method doesn't throw Exception
itself, the code catch(Exception e){
could still execute. 但是,即使我们知道该方法本身不会引发
Exception
,代码catch(Exception e){
仍然可以执行。 The code in the try
block could still throw something that inherits from Exception
. try
块中的代码仍然可以抛出从Exception
继承的内容。 That includes RuntimeException
and its subclasses, which are unchecked. 其中包括未经检查的
RuntimeException
及其子类。
catch(IOException e){
, on the other hand, can only catch checked exceptions. catch(IOException e){
只能捕获已检查的异常。 (Java doesn't allow multiple inheritance, so anything that's a subclass of IOException
can't possibly be a subclass of RuntimeException
.) The compiler can fairly easily figure out that none of the code in the try
block can possibly throw an IOException
(since any method throwing a checked exception must explicitly say so) which allows it to flag the code. (Java不允许多重继承,因此,作为
IOException
的子类的任何东西都不可能是RuntimeException
的子类。)编译器可以很容易地弄清楚try
块中的任何代码都不可能抛出IOException
(因为抛出检查异常的任何方法都必须明确地这样说),这允许它标记代码。
The behavior you're observing comes from the fact that the Java Language Specification treats Exception
specially in this circumstance. 您观察到的行为来自Java语言规范在这种情况下特别对待
Exception
的事实。 According to §11.2.3 : 根据§11.2.3 :
It is a compile-time error if a catch clause can catch checked exception class E 1 and it is not the case that the
try
block corresponding to thecatch
clause can throw a checked exception class that is a subclass or superclass of E 1 , unless E 1 isException
or a superclass ofException
.如果catch子句可以捕获 E 1的已检查异常类,并且不是
catch
子句所对应的try
块可以抛出 E 1的子类或超类的已检查异常类,则不是编译时错误。 E 1表示Exception
或类的超类Exception
。
This is reasonable because Exception
(and its superclass Throwable
) can be used to catch exceptions that extend RuntimeException
also. 这是合理的,因为
Exception
(及其超类Throwable
)可用于捕获也扩展RuntimeException
异常。 Since runtime exceptions are always possible, the compiler always allows Exception
to appear in a catch
clause regardless of the presence of checked exceptions. 由于运行时异常总是可能的,因此编译器始终允许
Exception
出现在catch
子句中,而不管是否存在已检查的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.