try catch finally return

结论:任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally存在的话。

如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,

编译器把finally中的return实现为一个warning。

但是需要注意的是,经过测试表明:在try 或者catch中的return语句,如果return的是int、long、double常量类型以及String、Integer等的封装类型,那么在finally中对return变量的值的修改不会影响在try 或者catch中的return语句的返回结果。

而恰好相反的是:在在try 或者catch中的return语句,如果return的是List等等对象类型的值,那么在finally中对其值的修改会影响try 或者catch中的return语句的返回结果。

当然, 如果在finally中执行return语句,finally对返回值的修改都会影响返回值。

举例说明1:

public static int tryCatchFinallyReturn(){
	    int a = 1;
		int b = 100;
		try{
			a = 2;
			return a+b-2;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("finally");
			a = 4;
			System.out.println(a);
			System.out.println("finally2");
		}
		return a+b;
	}
 main方法:
        int c = tryCatchFinallyReturn();
        System.out.print(c);

输出结果是:

finally
4
finally2
100

说明虽然a的值改为了4,但try中的返回值并没有被修改。

举例说明2:

	public static List tryTest(){
		List<Integer> list = new ArrayList<Integer>();
		int a = 2;
		try{
			list.add(new Integer(a));
			for(Integer ii :list){
				System.out.println(ii.intValue()+"----");
			}
			return list;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			System.out.println("finally");
			a = 4;
			System.out.println(a);
			list.add(new Integer(a));
			System.out.println("finally2");
			for(Integer ii :list){
				System.out.println(ii.intValue()+"----");
			}
		}
		return list;
	}
main方法:
		List<Integer> a =  tryTest();
		for(Integer b:a){
			System.out.println(b.intValue()+"----main");
		}

运行结果:

2----
finally
4
finally2
2----
4----
2----main
4----main

说明影响了对象类型的返回结果。

举例说明3:

	public static String trytestString(){
	    String a = "aaa";
		String b = "bbb";
		try{
			a = "aaa2";
			return a+b;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("finally");
			a = "aaa3";
			System.out.println(a);
			System.out.println("finally2");
		}
		return a+b;
	}
main方法:
System.out.println(trytestString());

输出:

finally
aaa3
finally2
aaa2bbb

跟返回常量结果一样。

版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/lingzhm

时间: 2024-08-26 17:52:50

try catch finally return的相关文章

(总结)try{}catch{}中有return,finally{}的执行情况

总结: 1.不管程序有没有bug,也不管try{}catch{}中有没有return语句,finally{}中的代码都会执行(记住这点). 2.先看例子...... 看下面的代码: class Program { static void Main(string[] args) { int x = 0; x = GetValue(); Console.WriteLine(" x的值为:" + x); Console.ReadKey(); } public static int GetVa

Java_try,catch,finally return之间的执行顺序

以往认为函数只要执行到return语句便会返回结果并终止,然而这时错误的,因为这存在特例. 掌握下面几条原则就可以完全解决“当try.catch.finally遭遇return”的问题. 原则:1.finally语句块中的代码是一定会执行的,而catch块中的代码只有发生异常时才会执行. 2. 函数执行完try块中的return语句后不会终止,还会继续执行catch(仅在抛出异常时执行).finally语句块. 3.函数必须确保有唯一返回值 说明: try中如果包含return语句则catch块

try catch finally return运行顺序

首先让我们搞懂两组概念:try catch finally和return 1.try catch finally 首先说try catch, (1)try语句 ,try语句用来包围可能出现异常的代码片段. try是发现问题的语句,发现异常后会跳入到catch{}中,如下: try{ 可能出现异常的代码片段 } (2)catch语句 ,catch语句是用来捕获try语句中出现的异常,并针对该异常解决的.catch语句块可以出现多次. catch(Exception_Type e){ 解决问题的代码

try-catch-finally 中哪个部分可以省略?try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗?

try-catch-finally 中哪个部分可以省略? finally这部分可以省略 try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗? 会执行 1.不管有没有异常,finally中的代码都会执行2.当try.catch中有return时,finally中的代码依然会继续执行3.finally是在return后面的表达式运算之后执行的,此时并没有返回运算之后的值,而是把值保存起来,不管finally对该值做任何的改变,返回的值都不会改变

try catch 与 return 和 finally 关系。

例如,我打开了一个不存在的文件import java.io.*;public class Demo {     public static void main(String[] args) {           try         {                       FileReader fr=new FileReader("d:\\aa.txt");         }         catch(Exception e)         {            

有return的情况下try catch finally的执行顺序

http://www.cnblogs.com/lanxuezaipiao/p/3440471.html?cm_mc_uid=89442383850615035911861&cm_mc_sid_50200000=1505491196 1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么

有return的情况下try catch finally的执行顺序(最有说服力的总结)

结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的:4.finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值.举例:情况1:try{}

try catch finally,try里有return,finally还执行么?

1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的:4.finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值.

有return如果是try catch finally运行命令

背景: 昨天一个朋友出去采访,遇到这样的问题:"C#  catch那里return.finally也弄它运行?" 个人总结实践: 1.无论有木有出现异常.finally块中代码都会运行. 2.当try和catch中有return时,finally仍然会运行. 详细案比例如以下(此处以没有返回值的函数进行验证): 3.假设是值传递.finally中改变的值对try或catch块中return返回的值无影响.假设是引用类型參数(地址传递或对象),finally中的值改变对return会产生