首页 > 技术文章 > Java:异常处理机制

Jiemyx 2021-04-27 22:27 原文

  • 捕获异常:try、catch、finally

  • 抛出异常:throw、throws


package com.jiemyx.throwable.demo01;

public class Test01 {
    public static void main(String[] args) {

        int a = 1;
        int b = 0;

        try {   //try:监控异常
            System.out.println(a/b);
        }catch (Exception e){   //catch:捕获异常
            System.out.println("捕获到异常,变量b不能为0");
        }finally {  //finally:处理善后工作,不管有没有异常,都会执行
            System.out.println("finally");
        }

        //finally可以不写,用于IO、资源的关闭等善后工作
    }
}

运行结果:

捕获到异常,变量b不能为0
finally


package com.jiemyx.throwable.demo01;

public class Test02 {
    public static void main(String[] args) {

        //假设要捕获多个异常,要从小到大
        /*
        Throwable > Error
        Throwable > Exception
        */

        try {
            new Test02().a();
        }catch (Exception e){   //catch(想要捕获的异常类型){}
            System.out.println("Exception异常");
        }catch (Error e){
            System.out.println("Error异常");
        }catch (Throwable e){
            System.out.println("Throwable异常");
        }finally {
            System.out.println("finally");
        }
    }

    public void a(){
        b();
    }

    public void b(){
        a();
    }
}

运行结果:

Error异常
finally


package com.jiemyx.throwable.demo01;

public class Test03 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        //捕获异常快捷键ctrl+alt+t
        try {
            System.out.println(a/b);
        } catch (Exception e) {
            e.printStackTrace();    //打印错误的栈信息
        } finally {
        }

    }
}

运行结果:

java.lang.ArithmeticException: / by zero
at com.jiemyx.throwable.demo01.Test03.main(Test03.java:10)


package com.jiemyx.throwable.demo01;

public class Test04 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        //throw 主动抛出异常,一般在方法体中使用
        try {
            if (b==0){
                throw new ArithmeticException();
            }
        }catch (ArithmeticException e){
            System.out.println("得到抛出的异常");
        }


    }
}

运行结果:

得到抛出的异常


package com.jiemyx.throwable.demo01;

public class Test05 {
    public static void main(String[] args) {

        new Test05().test(10,0);    //匿名类使用test()方法

    }

    public void test(int a,int b) {

        //方法中使用throw抛出异常,try-catch捕获处理异常
        if (b==0){
            try {
                throw new ArithmeticException();
            } catch (ArithmeticException e) {
                System.out.println("捕获到throw抛出的异常");
            }
        }else{
            System.out.println(a/b);
        }

    }

}

运行结果:

捕获到throw抛出的异常


package com.jiemyx.throwable.demo01;

public class Test06 {
    public static void main(String[] args) {
        //在方法调用处使用try-catch捕获处理异常
        try {
            new Test06().test(10,0);
        } catch (ArithmeticException e) {
            System.out.println("捕获到throws抛出的异常");
        }
    }

    //throws 抛出异常
    //如果在方法中,throw处理不了这个异常,那么就在方法上使用throws抛出异常,抛到方法调用处去处理
    public void test(int a,int b) throws ArithmeticException {
        if (b==0){
            throw new ArithmeticException();
        }
        System.out.println(a/b);
    }
}

运行结果:

捕获到throws抛出的异常


throw抛出就要使用try-catch处理,如果不处理需要添加throws

throws抛出不处理,抛到调用处使用try-catch去处理

推荐阅读