[英]Throw a checked exception
I need to modify the given method.我需要修改给定的方法。 'RunTimeException' is UNchecked... It should throw a checked exception:
'RunTimeException' 是未经检查的......它应该抛出一个检查的异常:
public class Main {
public static void method() {
throw RuntimeException;
}
public static void main(String[] args) {
try {
method();
} catch (RuntimeException e) {
System.out.println("RuntimeException");
} catch (Exception e) {
System.out.println("Exception");
}
}
}
I got error:我收到错误:
Compilation error
Main.java:4: error: cannot find symbol
throw RuntimeException;
^
symbol: variable RuntimeException
location: class Main
1 error
I strongly suggest reading tutorial here https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html .我强烈建议在这里阅读教程https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html 。
Here is the excerpt which relates to your queston这是与您的问题有关的摘录
Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception.
一般来说,不要仅仅因为您不想为指定方法可以抛出的异常而烦恼而抛出 RuntimeException 或创建 RuntimeException 的子类。这是底线准则:如果可以合理地期望客户端从异常,使其成为检查异常。 If.
如果。 a client cannot do anything to recover from the exception, make it an unchecked exception.
客户端无法从异常中恢复,使其成为未经检查的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.