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

finally是在try的语句执行之后并且try的return返回之前执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值,如test1测试代码),return的时候是复制了一个变量然后返回,所以之后finally操作的变量如果是基本类型的话不会影响返回值。 但是如果返回值是引用类型的话,因为指向同一个对象所以还是有影响的。在try块中有System.exit(0);这样的语句,System是一个Java类,调用exit(0)方法终止虚拟机也就是退出你的Java程序,括号里面的是参数,进程结束的返回值,这样finally语句也不会被执行到
 public static String test1() {
	        try {
	            System.out.println("try block");
	           return test12();
	      } finally {
	           System.out.println("finally block");
	       }
	  }
	  public static String test12() {
	       System.out.println("Start executing the ruturn statement in try");
	       return "try requires return statements";
	   }
运行的结果:(验证了finally是在开始执行try中要reuturn的语句之后并且try需要return的语句返回之前执行的。
try block
Start executing the ruturn statement in try
finally block
try requires return statement
finally块中有return语句的会覆盖try块中的return返回。如test2测试代码。
public static String test2() {
		String statements = "requires return statements";
		try {
			System.out.println("try block");
			return "try  " + statements;
		} catch (Exception e) {
			System.out.println("catch block");
		} finally {
			System.out.println("finally block");
			return "finally  " + statements;
		}
	}
运行结果:
try block
finally block
finally requires return statements
如果finally语句中没有return语句覆盖返回值,按照try块中的return返回 。如test3测试代码
public static void main(String[] args) {
		String returnState = test3();
		System.out.println(returnState);
	}

	public static String test3() {
		String statements = "requires return statements";
		String returnState = "";
		try {
			System.out.println("try block");
			returnState = "try " + statements;
			return returnState;
		} catch (Exception e) {
			System.out.println("catch block");
		} finally {
			System.out.println("finally block");
			statements = "finally " + statements;
		}

		return returnState;

	}
运行结果:
try block
finally block
try requires return statements
finally操作的变量如果是基本类型的话不会影响返回值,finally操作的变量如果返回值是引用类型的话,因为指向同一个对象所以还是有影响的如test4测试代码
 public static Map<String, String> test4() {
	        Map<String, String> map = new HashMap<String, String>();
	        map.put("KEY", "INIT");
	         
	        try {
	            map.put("KEY", "TRY");
	            return map;
	        }
	        catch (Exception e) {
	            map.put("KEY", "CATCH");
	        }
	        finally {
	            map.put("KEY", "FINALLY");
	            map = null;
	        }         
	        return map;
	    }
运行结果:
{KEY=FINALLY}
try块里的return语句在异常的情况下不会被执行。 如test5测试代码
public static String test5() {
		int b = 20;
		String statements = "requires return statements";
		try {
			System.out.println("try block");
			b = b / 0;
			return "try " + statements;
		} catch (Exception e) {
			b += 15;
			System.out.println("catch block");
		} finally {
			System.out.println("finally block");
			if (b > 25) {
				System.out.println("b>25, b = " + b);
			}
			b += 50;
		}
		return "Other statements ";
	}
运行结果:
try block
catch block
finally block
b>25, b = 35
Other statements 
当发生异常后,catch中的return执行情况与未发生异常时try中return的执行情况完全一样.如test6测试代码
public static String test6() {  
        int b = 20;  
        String statements = "requires return statements";
        try {  
            System.out.println("try block");  
              
            b = b /0;  
 
            return  "try " + statements;
        } catch (Exception e) {  
        	b += 15;
            System.out.println("catch block");  
            return  "catch " + statements;  
        } finally {  
            System.out.println("finally block");  
  
            if (b > 25) {  
                System.out.println("b>25, b = " + b);  
            }  
            b += 50;  
        }  
    }  
运行结果:
try block
catch block
finally block
b>25, b = 35
catch requires return statements

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值