who*_*ows -2 java exception-handling
try {
doSomething()
doSomethingElse()
}
catch(Exception e) {}
Run Code Online (Sandbox Code Playgroud)
如果doSomething()抛出异常,会doSomethingElse()被执行吗?如果没有,有没有办法让它被执行(但如果doSomethingElse抛出相同的异常,仍然捕获它)?
不,控制跳转到catch子句.
这是你可以轻松自己测试的东西......
public void doSomethingThatThrowsAnException() throws Exception {
throw new Exception();
}
try {
System.out.println("Before");
doSomethingThatThrowsAnException();
System.out.println("After");
} catch(Exception e) {
System.out.println("Caught");
}
Run Code Online (Sandbox Code Playgroud)
打印...
Before
Caught
Run Code Online (Sandbox Code Playgroud)