try-catch-finally-return的执行顺序和try-with-resource语法糖

try-catch-finally-return的执行顺序

总结如下几条情况

  1. ·try-catch-finally都有return语句时,没有异常时,返回值是finally中的return返回的·。

    • 执行try块,执行到return语句时,先执行return的语句
    • 但是不返回到main方法,执行finally块,遇到finally块中的return语句
    • 并将值返回到main方法,这里就不会再回去返回try块中计算得到的值
    • 所以最终的值是finally的返回
    • 举例如下
      public static void main(String[] args) throws ParseException {
          int i = NoException();
          System.out.println("最终值: " + i);
      }
      
      public static int NoException() {
          int i = 10;
          try {
              System.out.println("i in try block is:" + i);
              return --i;
          } catch (Exception e) {
              --i;
              System.out.println("i in catch - form try block is:" + i);
              return --i;
          } finally {
              System.out.println("i in finally - from try or catch block is:" + i);
              return --i;
          }
      }
      /*
      i in try block is:10
      i in finally - from try or catch block is:9
      最终值: 8
      
      */
      
  2. try-catch都有return语句时,没有异常时,返回值是try中的return返回的

    • try中执行完return的语句后,不返回,执行finally块
    • finally块执行结束后,返回到try块中
    • 最后,返回在try块中最后的值
    • 举例如下
      public static void main(String[] args) throws ParseException {
         int i = NoException();
         System.out.println("最终值: " + i);
      }
      
      public static int NoException() {
         int i=10;
         try{
             System.out.println("i in try block is:"+i);
             return --i;
         }
         catch(Exception e){
             --i;
             System.out.println("i in catch - form try block is:"+i);
             return --i;
         }
         finally{
             System.out.println("i in finally - from try or catch block is:"+i);
             --i;
             System.out.println("i in finally block is:"+i);
             //return --i;
         }
      }
      /*
      i in try block is:10
      i in finally - from try or catch block is:9
      i in finally block is:8
      最终值: 9
      */
      
  3. try块中抛出异常,try、catch和finally中都有return语句,返回值是finally中的return。

    • 抛出异常后,执行catch块,在catch块的return的执行完后
    • 并不直接返回而是执行finally,因finally中有return语句,所以,执行finally里面的语句
    • 最终返回finally的return
    • 举例如下
      public static void main(String[] args) throws ParseException {
          int i = NoException();
          System.out.println("最终值: " + i);
      }
      
      public static int NoException() {
          int i = 10;
          try {
              System.out.println("i in try block is:" + i);
              i = i / 0;
              return --i;
          } catch (Exception e) {
              System.out.println("i in catch - form try block is:" + i);
              --i;
              System.out.println("i in catch block is:" + i);
              return --i;
          } finally {
              System.out.println("i in finally - from try or catch block is--" + i);
              --i;
              System.out.println("i in finally block is--" + i);
              return --i;
          }
      }
      /*
      i in try block is:10
      i in catch - form try block is:10
      i in catch block is:9
      i in finally - from try or catch block is--8
      i in finally block is--7
      最终值: 6
      
      */
      
  4. try块中抛出异常,try和catch中都有return语句,返回的catch中return值。

    • 抛出异常后,执行catch块
    • 执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值。
    • 举例如下
      public static void main(String[] args) throws ParseException {
          int i = NoException();
          System.out.println("最终值: " + i);
      }
      
      public static int NoException() {
          int i = 10;
          try {
              System.out.println("i in try block is:" + i);
              i = i / 0;
              return --i;
          } catch (Exception e) {
              System.out.println("i in catch - form try block is:" + i);
              return --i;
          } finally {
      
              System.out.println("i in finally - from try or catch block is:" + i);
              --i;
              System.out.println("i in finally block is:" + i);
              //return i;
          }
      }
      /*
      i in try block is:10
      i in catch - form try block is:10
      i in finally - from try or catch block is:9
      i in finally block is:8
      最终值: 9
      */
      
  5. try、catch中都出现异常,在finally中有返回,返回finally中return值

    • try块中出现异常到catch,catch中出现异常到finally
    • finally中执行到return语句返回,不检查异常。
    • 举例如下
      public static void main(String[] args) throws ParseException {
          int i = NoException();
          System.out.println("最终值: " + i);
      }
      
      public static int NoException() {
          int i = 10;
          try {
              System.out.println("i in try block is:" + i);
              i = i / 0;
              return --i;
          } catch (Exception e) {
              System.out.println("i in catch - form try block is:" + i);
              int j = i / 0;
              return --i;
          } finally {
              System.out.println("i in finally - from try or catch block is:" + i);
              --i;
              System.out.println("i in finally block is:" + i);
              return --i;
          }
      }
      /*
      i in try block is:10
      i in catch - form try block is:10
      i in finally - from try or catch block is:10
      i in finally block is:9
      最终值: 8
      */
      

try-with-resource语法糖

JDK7之后,Java多了个新的语法:try-with-resources语句,该语句确保在语句执行完毕后,每个资源都被自动关闭 。

可以理解为是一个声明一个或多个资源的 try语句(用分号隔开),一个资源作为一个对象,并且这个资源必须要在执行完关闭的

其实之前的手动关闭,在这个语法糖里面并没有被取消,而是因为编译器,编译器自动帮我们生成了finally块,并且在里面调用了资源的close方法,所以例子中的close方法会在运行的时候被执行。

原理

传统写法

public void readFile() throws FileNotFoundException {
    FileReader fr = null;
    BufferedReader br = null;
    try{
        fr = new FileReader("d:/test.txt");
        br = new BufferedReader(fr);
        String s = "";
        while((s = br.readLine()) != null){
            System.out.println(s);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用当前语法

public void readFile() throws FileNotFoundException {
   try(
           FileReader fr = new FileReader("d:/test.txt");
           BufferedReader br = new BufferedReader(fr)
   ){
       String s = "";
       while((s = br.readLine()) != null){
           System.out.println(s);
       }
   } catch (IOException e) {
       e.printStackTrace();
   }
}
  • 11
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

?abc!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值