【发布时间】:2019-02-18 05:00:27
【问题描述】:
我正在学习 Java 中的异常,并且在此示例中遇到了编译错误:
public class FinallyExceptionExample {
public static void main (String[] args) throws Exception {
try {
System.out.println("1");
throw new Exception();
} finally{
System.out.println("3");
}
System.out.println("4");
}
}
例外是行的“无法访问的语句”:
System.out.println("4");
我想知道为什么我会收到这个错误,通常在最后我们继续执行代码之后?
【问题讨论】:
-
不,为什么在发生未捕获的异常后代码会正常继续执行?
-
你的方法抛出了一个异常并且没有捕捉到它,所以
try/finally块之后的任何东西都无法到达。 -
@khelwood 但最终块已执行。
-
是的,但是 finally 块没有捕获异常。它运行了,然后异常仍然没有被捕获,所以方法退出了。