finally

18 阅读1分钟

mp.weixin.qq.com/s/hHO0ChXNy… 异常处理高级 www.jianshu.com/p/ee8dcbb1b… blog.csdn.net/u010412719/… 字节码分析执行顺序

catch中抛出异常会执行finally吗? finally是退出方法栈帧之前执行吗? finally与return的优先级

public class Test {

	public static void main(String[] args) {
		Test t = new Test();

		try {
			t.method2();
		} catch (MyException2 e) {
			System.out.println(e.getMessage());
		}finally {
			System.out.println("f000");
		}
	}

	public void method2() throws MyException2 {
		try {
			method1();
		} catch (MyException1 e) {
			throw new MyException2("e222");
		}finally {
			System.out.println("f222");
		}
		System.out.println("m222");
	}

	public void method1() throws MyException1 {
		if (1 == 1) {
			throw new MyException1("e111");
		}
	}

	class MyException1 extends Exception {

		public MyException1(String message) {
			super(message);
		}

	}

	class MyException2 extends Exception {

		public MyException2(String message) {
			super(message);
		}

	}
}