public class HelloJava { public static int test() { try { System.out.println("hi, i am in try"); return 1; } catch(Exception e) { } finally { System.out.println("hi, i am in finally"); } return 2; } public static void main(String[] args) { System.out.println(test()); }}
运行结果:
hi, i am in tryhi, i am in finally1
当遇到exit:
public class HelloJava { public static int test() { try { System.out.println("hi, i am in try"); System.exit(0); } catch(Exception e) { } finally { System.out.println("hi, i am in finally"); } return 2; } public static void main(String[] args) { System.out.println(test()); }}
运行结果:
hi, i am in try