JAVA 方法中 try-catch-finally

public class MyTest{
	public static void main(String args[]) {
		System.out.println(test()); 
	}
	
	public static int test() {
		try {
			System.out.println("try被执行了!");
			return 1;
		}catch(Exception e) {
			System.out.println("catch被执行了!");
			e.printStackTrace();
			return 2;
		}finally {
			System.out.println("finally被执行了!");
			//return 3;
		}
	}
}

运行结果:

try被执行了!
finally被执行了!
1

当取消对finally内return 3;注释后,运行结果变为: 

try被执行了!
finally被执行了!
3

猜你喜欢

转载自blog.csdn.net/sinat_34637220/article/details/88946715