try_catch_finally的注意事项

 public class Test {
public static String str = "";
public static void fun(int i) {
try {
if(i == 1) {
throw new Exception();
}
str += "1";
} catch (Exception e) {
// TODO: handle exception
str += "2";
return;
}finally{

str += "3";
}
str += "4";
}
public static void main(String[] args) {
fun(0);
fun(1);
System.out.println(str);
}
}

最后打印的结果是13423。
先把finally中的代码执行一遍,再return。
还有注意的是,try块的时候,有异常抛出,则从抛出异常处跳出try块,开始查找匹配的catch。

你可能感兴趣的:(java,exception,catch,finally,try)