【发布时间】:2016-03-23 08:52:02
【问题描述】:
我有以下代码,它给了我一个编译错误。
// Program1 -- 编译错误
public class ExceptionExample {
public static void main(String[] a) {
try {
method();
} catch (ClassCastException p) {} catch (Exception e) {
System.out.println(" Exception");
}
}
public static void method() {
try {
throw new NullPointerException();
} finally {
System.out.println("Hello");
}
System.out.println("Hi");
}
}
但是在我添加了一些 catch 块之后,下面的代码就可以工作了。
// 程序 2 - 无编译错误
public class ExceptionExample {
public static void main(String[] a) {
try {
method();
} catch (ClassCastException p) {
} catch (Exception e) {
System.out.println(" Exception");
}
}
public static void method() {
try {
throw new NullPointerException();
}
// Below catch block has been added
catch (ClassCastException p) {
}
finally {
System.out.println("Hello");
}
System.out.println("Hi");
}
}
/////////////////////////////////////// ///////////// “System.out.println("Hi");”处的代码无法访问 我想知道,添加不必要的 catch 块如何解决我的问题?
【问题讨论】:
-
欢迎来到 StackOverflow!为了帮助您获得答复,请详细说明您希望回答的问题。您可以阅读this,了解如何提出最佳问题。
-
您遗漏了最重要的信息 - 您得到了什么实际的编译错误,错误信息是什么?
-
“System.out.println("Hi");”处的代码无法访问
标签: exception compiler-errors try-catch unreachable-code