简体   繁体   English

无需引发异常即可捕获,但IOException可以

[英]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 the catch clause can throw a checked exception class that is a subclass or superclass of E 1 , unless E 1 is Exception or a superclass of Exception . 如果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.

相关问题 未处理的异常:必须捕获或引发IOException - unhandled exception: IOException must be caught or thrown 未报告的异常IOException,必须捕获或声明为抛出 - Unreported exception IOException, must be caught or declared to be thrown 错误:未报告的异常IOException; 必须被捕获或声明被抛出流() - error: unreported exception IOException; must be caught or declared to be thrown in stream() Java错误未报告的异常IOException; 必须被抓住或宣布被抛出 - Java Error unreported exception IOException; must be caught or declared to be thrown Java错误:必须捕获未声明的异常ioexception或将其声明为引发 - Java error: unreported exception ioexception must be caught or declared to be thrown IOException; 必须被抓住或宣布被抛出 - IOException; must be caught or declared to be thrown IOException,必须被捕获或声明为抛出 - IOException, must be caught or declared to be thrown 预期的异常未捕获或未抛出? - Expected exception not caught or not thrown? Java类中的错误“必须捕获或声明抛出未报告的异常java.io.ioexception” - Error “unreported exception java.io.ioexception must be caught or declared to be thrown” in Java class 错误消息“未报告的异常 java.io.IOException;必须被捕获或声明为抛出” - Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM