在这段代码中,main 方法的 Catch 不会捕获运行时异常。执行finally块后,它应该已经转到main的异常块,但它没有。
class FinallyDemo {
static int m1(){
try{
System.out.println("Inside m1");
throw new RuntimeException("hi");
}
finally {
System.out.println("m1 finally");
return 5;
}
}
public static void main(String[] args) {
try{
System.out.println(m1());
}catch (Exception e){
System.out.println("main caught: "+ e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Inside m1
m1 finally
5
Run Code Online (Sandbox Code Playgroud)