异常处理(狂神)

异常处理(狂神)

  1. Error(错误)

  2. Arithmetic(算术异常)

  3. exception(异常)

  4. throwable(可划异常)–通过源码throwable是exception的类


    抛出异常:ctrl+alt+t

    执行异常顺序时多为:从1-4依次捕获(由2-4范围逐渐增幅),error放首位防止出现致命错误

    error:程序出现问题本身无法恢复,让程序终止(栈溢出等)

package exception;

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

        try { //try监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){ //捕获异常
            System.out.println("分母不为零");
        }finally { //善后工作(非必须)
            System.out.println("finally");
        }
    }
}
//catch (ArithmeticException e)->catch(想要捕捉的异常类型){}

5.主动异常

package exception;

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

        try { //try监控区域
            if (b==0) {
                throw new ArithmeticException();//主动抛出异常
            }
            System.out.println(a/b);

        }catch (Error e){
            System.out.println("Error");
        }catch (ArithmeticException e) {
            System.out.println("分母不为零");
        }catch (Exception e) {
            System.out.println("Exception");
        }catch (Throwable t) {
            System.out.println("Throwable");
        } finally {
            System.out.println("finally");
        }
    }
}

我们为什么会害怕?这是由于我们做事不能当机立断,一旦犹豫不决的时候,我们便会畏缩。但如果能够对某件事作明确的判断时,不论有无价值,我们都不会畏缩——李小龙

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值