【发布时间】:2015-03-11 16:38:02
【问题描述】:
我正在为 Java 助理考试而学习,并且在考试条件下正在努力确定哪些是已检查的,哪些是未检查的。我知道如果我可以根据我在这里阅读的内容编写代码,我可以查找它或执行 instanceof - 但是有没有一种简单的方法可以确定它。我捡了一些,但发现很难记住它们。下面是一个依赖于这些知识的问题的考试。
public static String readData (Path filePath)
throws IOException, IllegalArgumentException {
//implementation omitted
}
将编译哪两个代码片段?
public static void main(String[] args) {
try {
readData(Paths.get("test"));
} catch (IllegalArgumentException ex) {
System.err.println(ex);
}
}
public static void main(String[] args)
throws IOException{
readData(Paths.get("test.txt"));
}
public static void main(String[] args)
throws IllegalArgumentException{
readData(Paths.get("test.txt"));
}
public static void main(String[] args) {
readData(Paths.get("test.txt"));
}
public static void main(String[] args) {
try {
readData(Paths.get("test"));
} catch (IOException ex) {
System.err.println(ex);
}
}
【问题讨论】:
-
我不认为有什么办法可以让你牢记
RuntimeExceptions。但是有一些比其他更经常使用。标准的RuntimeExceptions 通常非常广泛,或者即使您不调用方法(NullPointerException、ArrayIndexOutOfBoundsException、ArithmeticException)也可能发生,检查异常往往更“专门”。 IE。IOException适用于 IO 上下文,但IllegalArgumentException适用于传递给方法的参数无效时。
标签: java exception checked-exceptions