zoukankan      html  css  js  c++  java
  • try catch finall 结构里的 return

     1 public class ExceptionDemo1 {
     2 
     3     public static void main(String[] args) {
     4         try {
     5             int b = 0;
     6             int res = 10/b;
     7             System.out.println(res);
     8         } catch (Exception e) {
     9             e.printStackTrace();
    10             System.out.println("catch...");
    11         }finally{
    12             System.out.println("finally...");
    13         }
    14         System.out.println("over");
    15     }
    16 }

    输出:
        java.lang.ArithmeticException: / by zero
        at test.exception.ExceptionDemo1.main(ExceptionDemo1.java:8)
        catch...
        finally...
        over

    相信这段代码,问题不大 


    throw和return:都可以结束方法

     1 public class ExceptionDemo3 {
     2 
     3     public static void main(String[] args) {
     4         try {
     5             int b = 0;
     6             int res = 10/b;
     7             System.out.println(res);
     8         } catch (Exception e) {
     9             System.out.println("catch...");
    10             throw new RuntimeException(e.getMessage());
    11             //System.out.println("catch...");
    12         }finally{
    13             System.out.println("finally...");
    14         }
    15         System.out.println("over");
    16     }
    17     
    18 }

    结果:

    1     catch...
    2     finally...
    3     Exception in thread "main" java.lang.RuntimeException: / by zero
    4         at test.exception.ExceptionDemo3.main(ExceptionDemo3.java:12)

    如果catch有throw语句,在catch块中throw后面的语句执行不到(报错)。
    此时仍然会执行finally语句,但不会执行finally后面的语句


     1 public class ExceptionDemo4 {
     2     
     3     public static void main(String[] args) {
     4 
     5         int i =test();
     6         System.out.println(i);
     7     }
     8     
     9     public static int test(){
    10         int res=0,b=0;
    11         
    12         try {
    13             res = 10/b;
    14         } catch (Exception e) {
    15             e.printStackTrace();
    16         }finally{
    17             res = 9;
    18             System.out.println("finally...");
    19         }
    20         return res;
    21 
    22     }
    23 
    24 }
    1     java.lang.ArithmeticException: / by zero
    2     finally...
    3         at test.exception.ExceptionDemo4.test(ExceptionDemo4.java:17)
    4         at test.exception.ExceptionDemo4.main(ExceptionDemo4.java:8)
    5     9
    6     

    这里没什么好说的


     1 public class ExceptionDemo6 {
     2     public static void main(String[] args) {
     3         
     4            int i =test();
     5            System.out.println(i);
     6 
     7     }
     8 
     9     public static int test(){
    10         int res=0,b=0;
    11         
    12         try {
    13             res = 10/b;
    14         } catch (Exception e) {
    15             System.out.println("catch");
    16             throw new RuntimeException(e.getMessage());
    17         }finally{
    18             res = 9;
    19             System.out.println("finally...");
    20         }
    21         return res;//不会执行
    22     }
    23 }

    结果:

    1     catch
    2     finally...
    3     Exception in thread "main" java.lang.RuntimeException: / by zero
    4         at test.exception.ExceptionDemo6.test(ExceptionDemo6.java:18)
    5         at test.exception.ExceptionDemo6.main(ExceptionDemo6.java:6)

    finally中的内容不论程序有无异常,都会被执行(除非在执行到finally之前jvm退出了),那么如果我们的程序在try和catch块中return了,finally中的还会执行吗?

    故意把filename写错,造出异常,输出为下:

    this is catch_for_filenot... block!
    this is finally block!
    this is main return value:false

    从这儿看出来,程序先输出catch块中的,后又去执行finally块中的,虽然在catch中已经返回了,最后执行mian方法中的,而且输出false,说明catch块中的也成功返回了。

    所以,面对疑问,我们可以很肯定的回答,即使有return语句,finally块也一定会被执行!


     1  
     2 
     3 public class FinallyDemo2 {
     4 
     5     public static void main(String[] args) {
     6 
     7        System.out.println(getInt());
     8 
     9     }
    10 
    11  
    12 
    13     public static int getInt() {
    14 
    15        int a = 10;
    16 
    17        try {
    18 
    19            System.out.println(a / 0);
    20 
    21            a = 20;
    22 
    23        } catch (ArithmeticException e) {
    24 
    25            a = 30;
    26 
    27            return a;
    28 
    29     /*
    30 
    31     * return a在程序执行到这一步的时候,
    32 
    33       这里不是return a而是return 30;这个返回路径就形成了。
    34 
    35     * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
    36 
    37        再次回到以前的返回路径,继续走return 30;
    38 
    39      */
    40 
    41        } finally {
    42 
    43            a = 40;
    44 
    45        }
    46 
    47           return a;
    49     }
    51 }

    可以尝试在finally中return a; 

    观察结果:返回40

    因为新的返回路径(return 40)生成,覆盖原来的return 30

    不积跬步无以至千里
  • 相关阅读:
    实例属性 类属性 实例域 类域
    研究数据集
    static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符
    accessor mothod mutator mothod 更改器方法 访问器方法 类的方法可以访问类的任何一个对象的私有域!
    上钻 下钻 切片 转轴 降采样
    识别会话
    Performance Tuning Using Linux Process Management Commands
    Secure Hash Algorithm 3
    grouped differently across partitions
    spark 划分stage Wide vs Narrow Dependencies 窄依赖 宽依赖 解析 作业 job stage 阶段 RDD有向无环图拆分 任务 Task 网络传输和计算开销 任务集 taskset
  • 原文地址:https://www.cnblogs.com/wihainan/p/4759797.html
Copyright © 2011-2022 走看看