异常的执行顺序

来源:互联网 发布:软件采购进度报表 编辑:程序博客网 时间:2024/06/07 05:06
public class ExceptionDemo {


/**
* @param args
*/
public static void main(String[] args) {
        int a = Test1();
        System.out.println(a);
    }


    public static int Test1(){
        int x = 5;
        try
        {
            int num = x / 0;
            System.out.println("try");
            return 1;
        }
        catch (ArithmeticException e) {
            System.err.println("除数不能为0!");
            return 4;
        }
        finally
        {
            ++x;
            System.out.println("finally");
            return 2;
        }

    }

}

直接上代码

执行顺序:try->发生异常处->catch->finally->return

其中不执行的:try的异常发生处后面的代码,如果finally里return了,其他的return不执行