java中try,finally和return
<span style="font-family: Arial, Helvetica, sans-serif;">public class Test {</span>
	static String s = "";

	public static void main(String args[]) {
		s = test1();
		System.out.println("8  " + s);
	}

	public static String test1() {

		try {
			System.out.println("try.....");
			return s = "a";
		} finally {
			s = "b";
			System.out.println("17  " + s);
		}
	}
}
这是finally中没有return的情况。结果:
try.....
17  b
8  a

当finally中有return时:

public class Test {
	static String s = "";

	public static void main(String args[]) {
		s = test1();
		System.out.println("8  " + s);
	}

	public static String test1() {

		try {
			System.out.println("try.....");
			return s = "a";
		} finally {
			return s = "b";;
		}
	}
}
结果:

try.....
8  b

分析如下:


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013351986/article/details/49963101
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

java中try,finally和return

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭