Java try catch finally执行顺序

package com.zzj.exception.demo1;

public class ExeOrder {
	
	static int i = 0;

	public static void main(String[] args) {
		System.out.println(getInt());
		System.out.println(i);
	}
	
	static int getInt(){
		try {
			i = 1;
			System.out.println("try");
			throw new Exception();
		} catch(Exception e){
			i = 2;
			System.out.println("catch");
			return i;
		} finally {
			i = 3;
			System.out.println("finally");
		}
	}

}

执行结果:

try
catch
finally
2
3

return 语句会将结果缓存,然后再执行finally。

你可能感兴趣的:(JavaSE)