异常基本使用

异常体系结构

在这里插入图片描述

JVM默认处理方案

在这里插入图片描述

try…catch

在这里插入图片描述

        try { //对可能出现异常的代码进行捕获
            method();
        }catch (Exception e){
            e.printStackTrace();//打印异常的信息
            System.out.println(e.getMessage());
            System.out.println(e.toString());
        }

Throwable

常用的成员方法

在这里插入图片描述

        try { //对可能出现异常的代码进行捕获
            method();
        }catch (Exception e){
            e.printStackTrace();//打印异常的信息
            System.out.println(e.getMessage());
            System.out.println(e.toString());
        }
java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at exception.Exception01.method(Exception01.java:34)
	at exception.Exception01.main(Exception01.java:10)
Index 3 out of bounds for length 3
java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

查看捕获的对象是否相同

public static void main(String[] args) {
        try {
            method();
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("b:"+e.hashCode()); //b:793589513
        }
    }
    public static void method() throws ArrayIndexOutOfBoundsException{
        try {
            int[] arr = {1,2};
            System.out.println(arr[2]);
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("a:"+e.hashCode()); //a:793589513
            throw e;
        }
    }

结论:相同

a:793589513
b:793589513

当出现下标越界异常时,会创建该异常类,会与catch关键字里的异常匹配,如果匹
配的话,将该引用赋值给变量e,throw关键字抛出的是该异常在内存中的引用地址。

编译时异常和运行时异常的区别

异常分为:编译时异常和运行时异常,也称为受检异常和非受检异常
没有继承RunTImeException的异常都为编译时异常
编译时异常的特点:
1.不进行处理没有办法通过编译
2.对异常进行处理,异常不一定会出现

public static void method1(){
        try {
            String s = "2021-08-09";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(s); //改方法会扔出异常,但不一定会出现
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

throw和throws关键字的区别

public static void main(String[] args) throws InterruptedException {
            method1(); //可以不对该方法进行try...catch处理
    }
    public static void method1(){
        //主动抛出异常
        
      for (int i = 0; i < 10; i++) {
            if (i == 7){
            //当i=7的时候主动抛出一个异常
                throw new ArrayIndexOutOfBoundsException("出现异常了");
            }
        }
    }
public static void main(String[] args) throws InterruptedException {
        try {
            method1();
        }catch (Exception e){
            System.out.println(e.getMessage());
            Thread.sleep(100000L);
        }
    }
    //声明该方法可能会出现ArrayIndexOutOfBoundsException异常,使用改方法
    //就必须进行try...catch处理
    public static void method1() throws ArrayIndexOutOfBoundsException{
        System.out.println("hhh");
    }
    public static void main(String[] args) {
        method(); //会出现运行时异常,没有对其进行处理
    }
    public static void method() throws ScoreException{
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入分数");
            int i = scanner.nextInt();
            if (i < 0 || i > 100){
                throw new ScoreException("分数错误!");
            }
        }
    }

总结:

	1.throw关键字用在方法的内部,当满足某些条件,主动抛出异常,可以不对改
	  方法进行处理
	2.当throw的是运行时异常是,可以指定throws抛出,也可以不用
	3.throws用于声明在方法上,表明改方法可能会出现该异常,使用该方法时必
	  须进行处理 
	4.当throws抛出的时运行时异常的话,可以不进行处理

自定义异常


public class MyException extends RuntimeException{
    public MyException() {
    }
    public MyException(String message) {
        super(message);//设置详情信息
    }
}

super(message)源码

   public Throwable(String message) {
        fillInStackTrace();
        detailMessage = message;
    }

finally关键字

无论method方法是否出现异常,finally块中的语句都会执行

        try {
            method(); //会出现运行时异常
        }catch (ScoreException e){
            System.out.println(e.getMessage());
        }finally {
            System.out.println("我会进行处理");
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值