Day07_Javas的异常处理(听得云里雾里,不会)

1.什么是异常

在这里插入图片描述

2.异常的分类

在这里插入图片描述

3.异常体系框架

在这里插入图片描述

4.Error错误(Error不是异常)与 Exception异常

在这里插入图片描述

在这里插入图片描述

5.异常的处理机制

//异常的处理机制: 抛出异常  捕获异常
//异常处理的五个关键字: try catch finally throw throws
//异常处理的快捷键生成方式 ctrl+alt+t 
public class Test {
    public static void main(String[] args) {
        int a =1;
        int b = 0;

        //finally可以不要,但是try catch 一定要
        try{//try可以监控区域,在try代码块中可以用catch来监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){//捕获异常,但必须先知道异常ArithmeticException 把它看做e
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//不管出不出异常,都要执行finally
            System.out.println("finally");
        }
    }
  • catch可以多个
public class Test {
    public static void main(String[] args) {
        int a =1;
        int b = 0;
        try{
            System.out.println(a/b);
        }catch (Error e){//catch(想要捕获的异常类型,最高型为Throwable 然后为Error,Exception)
            System.out.println("Error程序出现异常");
        }catch (Exception e) {//catch可以捕获多个异常,会层层递进捕获最合适的异常,要从小到大
            System.out.println("Exception程序出现异常");
        }
        catch (Throwable e){//catch要把最大的异常写在最后面
            System.out.println("Throwable程序出现异常");
        }
        finally {
            System.out.println("finally");
        }
    }
}
  • //主动抛出异常throw用在方法里面
    //主动抛出异常throws用在方法上
//主动抛出异常throw用在方法里面
//主动抛出异常throws用在方法上
public class Test3 {
    public static void main(String[] args) {

        try {
            new Test3().test(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        }
    }

    //假设这个方法中处理不了异常,方法上抛出异常
    public void test (int a,int b) throws ArithmeticException
    {
        if(b==0){//  throw在通常用在方法里面抛出异常
            throw new ArithmeticException();//主动抛出异常.通过throw来new一个方法报错的异常
        }
        System.out.println(a/b);
    }
}

6.自定义异常(用的不多,并且听不懂不会)

package com.exception;
//自定义异常要去继承Exception类
public class myException extends Exception{

    //传递数字 ,数字大于10抛出异常
    private int detail;

    public myException(int a) {
        this.detail = a;
    }

    @Override//异常的打印信息
    public String toString() {
        return "myException{"+"detail=" + detail + '}';
    }
}

public class Test {
    //可能会存在异常的方法
    static void test(int a) throws myException
    {
        System.out.println("传递的参数为:"+a);
        if(a>10)
        {
            throw new myException(a);
        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            test(15);
        } catch (myException e) {
            System.out.println("myException==>"+e);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值