Java异常之异常处理机制

异常处理机制

1、抛出异常

2、捕获异常

3、异常处理五个关键字:

try、catch、finally、throw、throws

注意:假设要捕获多个异常:需要按照层级关系(异常体系结构) 从小到大!

package exception;

/**
 * Java 捕获和抛出异常:
 * 异常处理机制
 * 1、抛出异常
 * 2、捕获异常
 * 3、异常处理五个关键字
 * try、catch、finally、throw、throws
 * 注意:假设要捕获多个异常:需要按照层级关系(异常体系结构) 从小到大!
 */
public class Test {

    public static void main(String[] args) {

        int a = 1;

        int b = 0;

        /**
         * try catch 是一个完整的机构体,finally 可以不要
         * 假设IO流,或者跟资源相关的东西,最后需要关闭,关闭的操作就放在 finally 中
         */
        try { //try 监控区域
            System.out.println(a / b);
        } catch (ArithmeticException exception){ //catch(想要捕获的异常类型) 捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        } finally { //处理善后工作
            System.out.println("finally");
        }

        System.out.println("-------------- 分隔符 --------------");

        try {
            new Test().a(); //无限循环
        } catch (Error error){
            System.out.println("Error");
        } catch (Exception exception){
            System.out.println("Exception");
        } catch (Throwable throwable){
            System.out.println("Throwable");
        } finally {
            System.out.println("finally");
        }

    }

    public void a(){
        b();
    }

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

捕获异常

快捷键:选中代码 Ctrl + Alt + T

捕获异常的好处:程序不会意外的停止,try catch 捕获异常后程序会正常的往下执行

package exception;

/**
 * 捕获异常快捷键
 * 选中代码后:Ctrl + Alt + T
 * 如:
 * 选中 System.out.println(a / b);
 * 然后快捷键 Ctrl + Alt + T
 */
public class Test2 {

    public static void main(String[] args) {

        int a = 1;
        int b = 0;

        try {
            System.out.println(a / b);
        } catch (Exception exception) {
            exception.printStackTrace(); //打印错误的栈信息
        } finally {
        }

    }
}

抛出异常

1、在方法中抛出异常:throw

2、在方法上抛出异常:throws

package exception;

/**
 * 捕获异常
 * 抛出异常
 */
public class Test3 {

    public static void main(String[] args) {

        /**
         * 方法中抛出异常
         */
        new Test3().test(1,0); //匿名内部类直接调用

        System.out.println("------------ 分隔符 -------------");

        /**
         * 方法上抛出异常
         * 捕获异常的好处:
         * 程序不会意外的停止,try catch 捕获异常后程序会正常的往下执行
         */
        try {
            new Test3().test2(1,0); //匿名内部类直接调用
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }

    }

    /**
     * 在方法中抛出异常:throw
     * @param a
     * @param b
     */
    public void test(int a, int b){
        if (b == 0){ //throw
            throw new ArithmeticException(); //主动抛出异常,一般在方法中使用
        }
        System.out.println(a / b);
    }

    /**
     * 假设在方法中处理不了这个异常,就在方法上抛出异常,然后捕获异常
     * 在方法上抛出异常:throws
     * @param a
     * @param b
     * @throws ArithmeticException
     */
    public void test2(int a, int b) throws ArithmeticException{
        if (b == 0){
            throw new ArithmeticException();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无明之徒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值