zoukankan      html  css  js  c++  java
  • 003-try-catch-finally-return执行顺序问题

    一、try-catch-finally-return执行顺序问题

    0、原始执行顺序

    try — > finally

    try —> catch —> finally

    1、try catch 中有 return,finally 中无 return,且 try 中无异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
        }
    }
    /**
    执行结果:
    try: i = 2
    finally: i = 5
    2
    */
    

    2、try catch finally中均有 return,try中无异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
            return i;
        }
    }
    /**
    返回结果5
    try: i = 2
    finally: i = 5
    5
    */
    

    3、try catch 中有 return,finally 中无 return,且 try 中有异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
        }
    }
    /**
    返回结果4
    exception: i = 4
    finally: i = 7
    4
    */
    

    4、try catch finally中均有 return,try中有异常抛出

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            System.out.println("finally: i = " + i);
            return i;
        }
    }
    /**
    返回结果5
    exception: i = 4
    finally: i = 7
    7
    */
    

    5、try、catch 中有 return,try finally 中有异常,finally 中的异常有 return

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            try {
                int b = i/0;
            } catch (Exception e) {
                 i = i + 5;
                 return i;
            }
            System.out.println("finally: i = " + i);
        }
    }
    /**
    exception: i = 4
    12
    */
    

    6、try、catch 中有 return,try finally 中有异常,finally 中的异常无 return

    public int add(){
        int i = 1;
        try{
            i = i + 1;
            int a = i/0;
            System.out.println("try: i = " + i);
            return i;
        }catch (Exception e){
            i = i + 2;
            System.out.println("exception: i = " + i);
            return i;
        }finally {
            i = i + 3;
            try {
                int b = i/0;
            } catch (Exception e) {
                 i = i + 5;
            }
            System.out.println("finally: i = " + i);
        }
    }
    /**
    exception: i = 4
    finally: i = 12
    4
    */
    

    总结

    • finally 的代码总会被执行
    • try、catch 中有 return 的时候,也会执行 finally。return 的时候,要注意返回值是否会受到 finally 中代码的影响。
    • finally 中有 return 的时候,会直接在 finally 中退出,导致 try、catch中的 return 失效。
    • finally 中如果有异常的时候,且存在 return 的时候,catch 的 return 会被覆盖。
  • 相关阅读:
    深度讲解Go语言-学习笔记
    vagrant常用命令
    CentOS7 安装Python虚拟环境 virtualenvwrapper
    《Android开发艺术探索》读书笔记——Cha3.2.3改变布局参数实现View的滑动
    Map接口的实现类 Map的区别
    Java多线程之内存可见性和原子性操作 一 synchronized
    LeetCode 153. Find Minimum in Rotated Sorted Array
    LeetCode 64. Minimum Path Sum
    实现线程同步的几种方式
    IOC的底层原理
  • 原文地址:https://www.cnblogs.com/ice-image/p/14512269.html
Copyright © 2011-2022 走看看