zoukankan      html  css  js  c++  java
  • 我敢说你不一定完全理解try 块,catch块,finally 块中return的执行顺序

    大家好,今天我们来讲一个笔试和面试偶尔都会问到的问题,并且在工作中不知道原理,也会造成滥用。

    大家可能都知道,try 块用来捕获异常,catch块是处理try块捕获的异常,finally 块是用来关闭资源。一个try块后面可以跟多个catch块,如果后面一个catch块也不跟,就一定要跟一个finally 块。

    结论1:当在try块遇到return语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

    public class FinallyTest {
        public static void main(String[] args) throws Exception {
            int a = 10;
            int sum = throwException(a);
            System.out.println("执行返回结果sum:" + sum);
        } 
        
        public static int throwException(int a) throws Exception{
            int b = 20;
            int result = 0;
            try{
                System.out.println("执行try语句块");
                result = a+b;
                return result;
            }catch(Exception e){
                System.out.println("执行catch语句块");
            }finally{
                System.out.println("执行finally语句块");
                result = 1;
            }

            return result;

        }
    }

    结论2:当在catch块遇到return或者throw异常语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

    public class FinallyTest {
        public static void main(String[] args) throws Exception {
            int a = 10;
            int sum = throwException(a);
            System.out.println("执行返回结果sum:" + sum);
        } 
        
        public static int throwException(int a) throws Exception{
            int b = 20;
            int result = 0;
            try{
                System.out.println("执行try语句块");
                result = b / 0;
                return result;
            }catch(Exception e){
                System.out.println("执行catch语句块");
                return result;
            }finally{
                System.out.println("执行finally语句块");
                result = 1;
            }
            
        }
    }

    结论3:如果try,finally语句里均有return,忽略try的return值,而使用finally的return值

    public class FinallyTest {
        public static void main(String[] args) throws Exception {
            int a = 10;
            int sum = throwException(a);
            System.out.println("执行返回结果sum:" + sum);
        } 
        
        public static int throwException(int a) throws Exception{
            int b = 20;
            int result = 0;
            try{
                System.out.println("执行try语句块");
                result = a +  b;
                return result;
            }catch(Exception e){
                System.out.println("执行catch语句块");
            }finally{
                System.out.println("执行finally语句块");
                result = 1;
                
                return result;
            }
            
        }
    }

    结论4 :在finally语句块发生了异常,finanly语句块中异常后面的代码不会再执行。

    public class FinallyTest {
    	public static void main(String[] args) throws Exception {
    		int a = 10;
    		int sum = throwException(a);
    		System.out.println("执行返回结果sum:" + sum);
    	} 
    	
    	public static int throwException(int a) throws Exception{
    		int result = 30;
    		try{
    			System.out.println("执行try语句块");
    			return result;
    		}catch(Exception e){
    			System.out.println("执行catch语句块");
    		}finally{
    			int exception = b / 0;
    			System.out.println("执行finally语句块");
    			result = 1;
    		}
    		return result;
    	}
    }

    结论5:try语句块发生异常,并且finally语句块也发生了异常,finally块中的异常会掩盖try块中的异常。

    public class FinallyTest {
    	public static void main(String[] args) throws Exception {
    		int a = 10;
    		int sum = throwException(a);
    		System.out.println("执行返回结果sum:" + sum);
    	} 
    	
    	public static int throwException(int a) throws Exception{
    		int result = 0;
    		try{
    			System.out.println("执行try语句块");
    			result = a / 0 ;
    			return result;
    		}catch(Exception e){
    			throw new Exception(e);
    		}finally{
    			int[] arr = new int[1];
    			arr[2] = 3;
    			System.out.println("执行finally语句块");
    		}
    	}
    }
    

      

    通过上面的5个案例,我们应该对try 块,catch块,finally 块中return的执行顺序有了清晰的理解,下面我们在进行总结一下。

    1. 当在try块遇到return语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

    2. 当在catch块遇到return或者throw异常语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响。

    3. 如果try,finally语句里均有return,忽略try的return,而使用finally的return。

    4 . 在finally语句块发生了异常,finanly语句块异常后面的代码不会在执行。

    5:try{}语句块发生异常,并且finally语句块也发生了异常,finally块中的异常会掩盖try块中的异常。

    另外,关于1,2两点,不受finally块影响的原理,我做一个补充,为什么在finally块改变try块中的返回值,结果不受影响了?

    如果try语句里有return,返回的是try语句块中的局部变量值。 详细执行过程如下:

    ①首先我们在try快中,会把返回值保存到局部变量中;

    ② 然后执行jsr指令跳到finally语句里执行;

    ③ 执行完finally语句后,返回之前保存在局部变量表里的值,所以finally块里改变try块中的返回值,不会生效。(但是上面第3点场景,finally自己带return返回值除外)

    感谢大家的观看,如有错误之处,欢迎指出,共同学习,共同进步~

    如果大家看我的文章觉得有所收获,请将文章分享给你们的朋友,后续我会更新更多浅显,优质的技术文章!

     
  • 相关阅读:
    Ansible概述
    iptables端口转发
    iptables配置实例
    iptables常用操作
    iptables常用命令
    每日总结3.15
    每日总结3.12
    每日总结3.11
    每日总结3.10
    每日总结3.9
  • 原文地址:https://www.cnblogs.com/haly/p/10724133.html
Copyright © 2011-2022 走看看