Java try/catch/finally内部执行顺序&外部语句何种情况下执行

版权声明:本文为博主原创文章,转载请注明出处 浅然的专栏 https://blog.csdn.net/w_linux/article/details/80589486

该篇博客说说try/catch语句的一些执行逻辑,因为有很多人并没有弄清楚到底try、catch、finally语句是怎么样执行的。

一、try/catch/finally中的语句执行先后

1、如果catch中有return或者throw,执行顺序如下

  • catch中的非return或thorw语句
  • finally中的非return或throw语句
  • catch中的return或throw语句

    package testexception;

    public class testException {
    public static void main(String[] args) {
    //测试
    double sum = test();
    System.out.println(sum);
    }

    public static double test() {
        double sum = 0;
        try {
            sum = 1/0;
            System.out.println("try....");  //不执行
        }catch(ArithmeticException e) {
            System.out.println("message:"+e.getMessage());
            return 1.0;
            //throw e;
        }finally{
            System.out.println("finally....");
        }
        return 2.0;                         //不执行
    }
    

    }

运行结果:

这里写图片描述

2、如果catch中有return或者throw,并且finally中也有return或throw,执行顺序如下

  • catch中的非return或thorw语句
  • finally中的非return或throw语句
  • catch中的return或throw语句
  • finally中的return或throw语句

    package testexception;
    
    public class testException {
        public static void main(String[] args) {
            //测试
            double sum  = test();
            System.out.println(sum);
        }
    
        public static double test() {
            double sum = 0;
            try {
                sum = 1/0;
                System.out.println("try....");  //不执行
            }catch(ArithmeticException e) {
                System.out.println("message:"+e.getMessage());
                return 1.0;
                //throw e;
            }finally{
                System.out.println("finally....");
                return 2.0;
            }
        }
    }
    

运行结果:(可以看出最后返回值为2.0)

这里写图片描述


二、什么情况下try/catch/finally语句块之后的语句会执行

1、进行正常捕获处理时:会执行

package testexception;

public class testException {
    public static void main(String[] args) {
        //测试
        double sum  = test();
        System.out.println(sum);
    }

    public static double test() {
        double sum = 0;
        try {
            sum = 1/0;
            System.out.println("try....");  //不执行
        }catch(ArithmeticException e) {
            System.out.println("捕获处理中....");
        }finally{
            System.out.println("finally....");
        }
        System.out.println("看看try/catch/finally外面的语句执不执行"); 
        return 3.0;
    }
}

运行结果:

这里写图片描述

2、捕获后又抛出:不执行

package testexception;

public class testException {
    public static void main(String[] args) {
        //测试
        double sum  = test();
        System.out.println(sum);
    }

    public static double test() {
        double sum = 0;
        try {
            sum = 1/0;
            System.out.println("try....");  //不执行
        }catch(ArithmeticException e) {
            System.out.println("捕获处理中....");
            throw e;
        }finally{
            System.out.println("finally....");
        }
        System.out.println("看看try/catch/finally外面的语句执不执行"); 
        return 3.0;
    }
}

运行结果:

这里写图片描述

3、比如是一个ArithmeticException异常换成其他异常(非Exception):不执行

package testexception;

public class testException {
    public static void main(String[] args) {
        //测试
        double sum  = test();
        System.out.println(sum);
    }

    public static double test() {
        double sum = 0;
        try {
            sum = 1/0;
            System.out.println("try....");  //不执行
        }catch(NullPointerException e) {
            System.out.println("捕获处理中....");
        }finally{
            System.out.println("finally....");
        }
        System.out.println("看看try/catch/finally外面的语句执不执行"); 
        return 3.0;
    }
}

运行结果:

这里写图片描述

当然最后要注意的是try语句块中异常出现的位置,其在try中之后的语句都不会执行

猜你喜欢

转载自blog.csdn.net/w_linux/article/details/80589486
今日推荐