zoukankan      html  css  js  c++  java
  • 论 异常处理机制中的return关键字

    Java中,执行try-catch-finally语句需要注意:

    第一:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂存在栈里面,等待finally执行后再返回)

    第二:finally里面不建议放return语句,根据需要,return语句可以放在try和catch里面和函数的最后。可行的做法有四种:   

      1)return语句只在方法最后出现一次。   

      2)return语句仅在try和catch里面都出现。   

      3)return语句仅在try和方法最后都出现。   

      4)return语句仅在catch和方法的最后都出现。

    注意,除此之外的其他做法都是不可行的,编译器会报错。

      (1)如果程序运行到try成功时可以返回结果,则采用方法2。

      (2)如果程序运行到catch时(即中途出错时)无需再继续执行后面的代码了,则采取方法4。

      (3)如果程序运行到try或catch时还需要继续执行后面的代码,则采取方法1。

    demo 1:

    package cn.day006;
    
    public class Test {
    
         public static void main(String[] args) {
                System.out.println(getResult());
            }
    
            @SuppressWarnings("finally")
            public static int getResult(){
                int a =100;
                
                try{
                    return a+1; //注意,java的基础数据类型是值传递,这里的返回值已经和上面的a没有关系了
                }catch(Exception e){
                    e.printStackTrace();  
                }finally{
                    return a;    //最后再把值重定向到a(相当于将try中的返回值覆盖掉),所以输出还是100
                }
            }
    }

    执行结果:100

    demo 2:

    package cn.day006;
    
    public class Test {
    
         public static void main(String[] args) {
                System.out.println(getResult());
            }
    
            @SuppressWarnings("finally")
            public static int getResult(){
                int a =100;
                
                try{                
                    return a++;   //切记 a++   ----> a=a+1 此时a的值做出了改变
                }catch(Exception e){
                    e.printStackTrace();  
                }finally{
                    return a;   
                }
            }
    }

    执行结果:101

    demo 3:

    package cn.day006;
    
    public class Test {
    
         public static void main(String[] args) {
                System.out.println(getResult());
            }
            public static int getResult(){
                int a =100;            
                try{
                      a++;
                    return a;              
                }finally{
                    a++;   
                }
            }
    }

    结果是101。

    分析:

    在try语句中,在执行return语句时,要返回的结果已经准备好了,就在此时,程序转到finally执行了。

    在转去之前,try中先把要返回的结果存放到不同于x的局部变量中去,执行完finally之后,在从中取出返回结果,

    因此,即使finally中对变量x进行了改变,但是不会影响返回结果。

    它应该使用栈保存返回值。

    demo 4:

    package cn.day006;
    
    public class Test2 {
         public static void main(String[] args) {
                try{
                System.out.println(getResult());
                }catch(Exception e){
                    e.printStackTrace();
                    System.out.println("截获异常catch");
                }finally{
                    System.out.println("异常处理finally");
                }
            }
    
            @SuppressWarnings("finally")
            public static int getResult() throws Exception{
                int a =100;            
                try{                
                    a=a+10; 
                    throw new RuntimeException();
                }catch(Exception e){
                    System.out.println("截获异常并重新抛出异常");
                    throw new Exception();            
                }finally{
                    return a;
                }
            }
    }

    结果如下:

    截获异常并重新抛出异常
    110
    异常处理finally

    关键的“截获异常catch”却没有执行!!!

    原因是在getResult()的finally中return一个值,等同于告诉编译器该方法没有异常,但实际上异常是有的,这样的结果是该方法的调用者却捕获不到异常,相对于异常被无端的被吃掉了,隐藏杀机啊!!

    结论:不要再finally中试图return一个值,这样可能会导致一些意想不到的逻辑错误,finally就是用来释放资源的!!

    参考牛人博客:http://blog.csdn.net/z69183787/article/details/61923256

  • 相关阅读:
    HDU 6071
    HDU 6073
    HDU 2124 Repair the Wall(贪心)
    HDU 2037 今年暑假不AC(贪心)
    HDU 1257 最少拦截系统(贪心)
    HDU 1789 Doing Homework again(贪心)
    HDU 1009 FatMouse' Trade(贪心)
    HDU 2216 Game III(BFS)
    HDU 1509 Windows Message Queue(队列)
    HDU 1081 To The Max(动态规划)
  • 原文地址:https://www.cnblogs.com/lsy131479/p/8513577.html
Copyright © 2011-2022 走看看