try{ } catch{ } finally{ } 语句的执行流程
今天群里讨论了一个问题:try{ } catch{ } finally{ } 语句的执行流程!
在这里做下记录:
【程序清单1】:
public static void main(String[] args){
try {
System.out.println("try");
} catch (Exception e) {
System.out.println("catch");
} finally {
System.out.println("finally");
}
System.out.println("other");
}

【程序清单2】:
public static void main(String[] args){
String s = null;
try {
System.out.println("try");
s.equals(""); // 让catch 捕获一个 NullPointerException对象;
} catch (Exception e) {
System.out.println("catch");
} finally {
System.out.println("finally");
}
System.out.println("other");
}

【程序清单3】:
public static void main(String[] args) throws Exception{
String s = null;
try {
System.out.println("try");
s.equals(""); // 让catch 捕获一个 NullPointerException对象;
} catch (Exception e) {
System.out.println("catch");
throw new Exception(e); // 或者 throw e;
} finally {
System.out.println("finally");
}
System.out.println("other");
}
