多次抛出的异常是检查还是运行时?

发布于 2024-08-31 12:26:51 字数 983 浏览 12 评论 0原文

我有一个异常链,其中 method1method2 抛出异常,而 method2 又向 main 抛出异常。由于某种原因,编译器强制我处理 method2 中的错误,如果我不这样做,则将其标记为错误,表明这是一个已检查的异常。但是,当同样的Exception被进一步抛出到main时,编译器允许我忽略它并且不显示任何错误。

method1 中的原始异常是 ParseException,已检查。但该方法在标头中有一个通用的 throws Exception 子句,并且将相同的对象抛出到 method2,该方法具有相同的 throws Exception 子句。这个异常何时以及如何失去被编译器检查/捕获的状态?

编辑以澄清:

public void method1() throws Exception{
   // code that may generate ParseException
}

public void method2() throws Exception{
   method1(); //compiler error (if the throws clause is left out)
}

public static void main(String[] args){
   method2(); //ignored by compiler, even though the exception isn't caught or thrown or handled at all
}

编辑: 抱歉大家,这个问题是基于一个错误...... main 方法实际上有一个我丢失的 throws Exception 子句。我已经删除了它,代码现在的行为符合预期。感谢您的帮助!

I have an Exception chain in which method1 throws an Exception to method2 which throws the Exception on to main. For some reason, the compiler forces me to deal with the error in method2 and marks it as an error if I don't, indicating that it's a checked Exception. But when the same Exception is thrown further down the line to main, the compiler allows me to ignore it and doesn't display any errors.

The original Exception in method1 is a ParseException, which is checked. But the method has a generic throws Exception clause in the header, and the same object is thrown to method2, which has an identical throws Exception clause. When and how does this Exception lose the status of being checked / caught by the compiler?

Edited to clarify:

public void method1() throws Exception{
   // code that may generate ParseException
}

public void method2() throws Exception{
   method1(); //compiler error (if the throws clause is left out)
}

public static void main(String[] args){
   method2(); //ignored by compiler, even though the exception isn't caught or thrown or handled at all
}

Edit:
Sorry everyone, the question was based on a mistake... The main method actually had a throws Exception clause that I was missing. I've removed that and the code is now behaving as expected. Thanks for all the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

优雅的叶子 2024-09-07 12:26:52

是否检查异常完全取决于异常类型:如果是 RuntimeException 或其子类,则不检查;如果是 RuntimeException ,则不检查异常。否则,就是这样。 (是的,RuntimeExceptionException 的子类——Java 库设计的失败之一,但不是最严重的。)

编译器检查的是方法签名。因此,实际抛出的异常是无关紧要的(为此目的)。如果方法说抛出异常,那么您必须在方法中捕获异常,或者声明该方法抛出异常。方法应始终使用尽可能窄的 throws 子句 - 例如,不是 抛出异常,而是抛出 ParseException

(我说“无关(为此目的)”是因为,当然,编译器要做的其他事情之一是检查您是否没有抛出 未涵盖的已检查异常code>throws 子句。)

编辑您在编辑中添加的代码将无法编译:1. 它在没有实例的情况下调用实例方法,并且 2. main 需要声明它抛出Exception

此代码解决了其他问题,并且(正确地)演示了 main 需要 throws Exception 子句:

public class CheckTest
{
    public static final void main(String[] params)
    {
        new CheckTest().method2();
    }

    public void method1() throws Exception{
        throw new java.text.ParseException("foo", 2);
    }

    public void method2() throws Exception{
        this.method1();
    }
}

结果:

CheckTest.java:27: unreported exception java.lang.Exception; must be caught or declared to be thrown

                new CheckTest().method2();
                                       ^
1 error

Whether an exception is checked or not is entirely dependent on what kind of exception it is: If it's a RuntimeException or a subclass of it, it's not checked; otherwise, it is. (And yes, RuntimeException is a subclass of Exception — one of the failures of the Java library design, but not the most major.)

What the compiler checks is the method signatures. So the actual exception thrown is irrelevant (for this purpose). If the methods say throws Exception then you have to catch Exception in your method or declare that the method throws Exception. Methods should always use the narrowest possible throws clause — e.g., not throws Exception but throws ParseException.

(I say "irrelevant (for this purpose)" because, of course, one of the other things the compiler will do is check that you don't throw checked exceptions that aren't covered by your throws clause.)

Edit The code you added in your edit won't compile: 1. It's calling an instance method without an instance, and 2. main needs to declare that it throws Exception.

This code solves the other problems, and (correctly) demonstrates that main needs the throws Exception clause:

public class CheckTest
{
    public static final void main(String[] params)
    {
        new CheckTest().method2();
    }

    public void method1() throws Exception{
        throw new java.text.ParseException("foo", 2);
    }

    public void method2() throws Exception{
        this.method1();
    }
}

Result:

CheckTest.java:27: unreported exception java.lang.Exception; must be caught or declared to be thrown

                new CheckTest().method2();
                                       ^
1 error
忆悲凉 2024-09-07 12:26:52

检查的异常不会停止是检查的异常。将已检查异常转换为未检查异常的方法是将其包装在 RuntimeException 类型中并重新抛出它。

A checked exception does not stop being a checked exception. The way you can sort of turn a checked exception into unchecked is by wrapping it inside a type of RuntimeException and re-throwing it.

嘿嘿嘿 2024-09-07 12:26:52

就您而言,这将是异常一直向下 到调用堆栈的根。如果一个方法被声明为抛出Exception(这种方法很少应该这样做),则调用方法要么必须说它也抛出Exception,要么捕获它并处理它。

编辑 至于编辑后的OP中的示例,将无法编译。不过,如果您不想处理它,可以声明 main 来抛出 Exception

In your case, it'll be Exceptions all the way down to the root of the call stack. If a method is declared to throw Exception (which methods very seldom should do), the calling method will either have to say it throws Exception too, or catch it and deal with it.

Edit As for the example in the edited OP, that won't compile. You can declare main to throw Exception if you don't want to deal with it, though.

水中月 2024-09-07 12:26:52

您的编译器是否有可能没有抱怨 main() 中的问题,因为它遇到了 method2() 中的问题并在此时停止检查语法?两者都应该是一个错误。您是否修复了 method2() 中的调用并获得了干净的编译?

Is it possible that your compiler is not complaining about the issue in main() because it hits the issue in method2() and stops checking the syntax at that point? Both should be an error. Have you fixed the call in method2() and gotten a clean compile?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文