多次抛出的异常是检查还是运行时?
我有一个异常链,其中 method1
向 method2
抛出异常,而 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是否检查异常完全取决于异常类型:如果是 RuntimeException 或其子类,则不检查;如果是 RuntimeException ,则不检查异常。否则,就是这样。 (是的,
RuntimeException
是Exception
的子类——Java 库设计的失败之一,但不是最严重的。)编译器检查的是方法签名。因此,实际抛出的异常是无关紧要的(为此目的)。如果方法说
抛出异常
,那么您必须在方法中捕获异常
,或者声明该方法抛出异常
。方法应始终使用尽可能窄的throws
子句 - 例如,不是抛出异常
,而是抛出 ParseException
。(我说“无关(为此目的)”是因为,当然,编译器要做的其他事情之一是检查您是否没有抛出 未涵盖的已检查异常code>throws 子句。)
编辑您在编辑中添加的代码将无法编译:1. 它在没有实例的情况下调用实例方法,并且 2.
main 需要声明它抛出
Exception
。此代码解决了其他问题,并且(正确地)演示了
main
需要throws Exception
子句:结果:
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 ofException
— 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 catchException
in your method or declare that the methodthrows Exception
. Methods should always use the narrowest possiblethrows
clause — e.g., notthrows Exception
butthrows 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 throwsException
.This code solves the other problems, and (correctly) demonstrates that
main
needs thethrows Exception
clause:Result:
检查的异常不会停止是检查的异常。将已检查异常转换为未检查异常的方法是将其包装在 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.
就您而言,这将是
异常
一直向下 到调用堆栈的根。如果一个方法被声明为抛出Exception
(这种方法很少应该这样做),则调用方法要么必须说它也抛出Exception
,要么捕获它并处理它。编辑 至于编辑后的OP中的示例,将无法编译。不过,如果您不想处理它,可以声明
main
来抛出Exception
。In your case, it'll be
Exception
s all the way down to the root of the call stack. If a method is declared to throwException
(which methods very seldom should do), the calling method will either have to say it throwsException
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 throwException
if you don't want to deal with it, though.您的编译器是否有可能没有抱怨
main()
中的问题,因为它遇到了method2()
中的问题并在此时停止检查语法?两者都应该是一个错误。您是否修复了method2()
中的调用并获得了干净的编译?Is it possible that your compiler is not complaining about the issue in
main()
because it hits the issue inmethod2()
and stops checking the syntax at that point? Both should be an error. Have you fixed the call inmethod2()
and gotten a clean compile?