规则一
- return和throw等价,都会结束方法;
- 程序运行时在catch中遇到return或者throw后,会先运行finally代码块所有的代码,包括return和throw;
- 综上return 和 throw new Exception()不能同时运行;
示例一:finally中有return/throw导致catch中return未运行
public class TryCatchFinally {
private String testTry(){
try{
System.out.println("try");
}catch (Exception x){
System.out.println("catch");
throw new RuntimeException("catch");//不会运行
}
finally {
System.out.println("finally");
return "finally";
}
}
public static void main(String[] args) {
System.out.println(new TryCatchFinally().testTry());
}
}