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

    不积跬步无以至千里
  • 相关阅读:
    刷题-力扣-700. 二叉搜索树中的搜索
    作业要求20191010-8 alpha week 1/2 Scrum立会报告+燃尽图 06
    20191010-7 alpha week 1/2 Scrum立会报告+燃尽图 05
    20191010-6 alpha week 1/2 Scrum立会报告+燃尽图 04
    20191010-5 alpha week 1/2 Scrum立会报告+燃尽图 03
    20191010-4 alpha week 1/2 Scrum立会报告+燃尽图 02
    20191010-3 alpha week 1/2 Scrum立会报告+燃尽图 01
    扛把子组Scrum立会报告+燃尽图 07
    扛把子组20180926-1 选题展示
    "PSP助手”微信小程序宣传视频链接及内容介绍
  • 原文地址:https://www.cnblogs.com/wihainan/p/4759797.html
Copyright © 2011-2022 走看看