Java 之 异常

异常

异常指程序在运行时出现错误时通知调用者的一种机制。

1.异常的体系

在这里插入图片描述
顶层类 Throwable 派生出两个重要的子类, Error 和 Exception
Error类和Exception类又派生出许多子类

  • Error 指的是 Java 运行时内部错误和资源耗尽错误. 应用程序不抛出此类异常. 这种内部错误一旦出现, 除了告知用户并使程序终止之外, 再无能无力. 这种情况很少出现.
  • Exception 是我们程序员所使用的异常类的父类. 其中 Exception 有一个子类称为RuntimeException , 这里面又派生出很多我们常见的异常类 NullPointerException , IndexOutOfBoundsException 等.
  • 异常又分为编译时异常和运行时异常。

2.处理异常的流程

  • 程序先执行 try 中的代码
  • 如果 try 中的代码出现异常, 就会结束 try 中的代码, 看和 catch 中的异常类型是否匹配.
  • 如果找到匹配的异常类型, 就会执行 catch 中的代码 如果没有找到匹配的异常类型, 就会将异常向上传递到上层调用者.
  • 无论是否找到匹配的异常类型, finally 中的代码都会被执行到(在该方法结束之前执行).
  • 如果上层调用者也没有处理的了异常, 就继续向上传递.
  • 一直到 main 方法也没有合适的代码处理异常, 就会交给 JVM 来进行处理, 此时程序就会异常终止.
1.不处理异常

若在代码执行时,未捕获处理异常,则交由JVM处理异常,一旦执行到异常处,程序终止。

  //不处理异常
    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4};
        System.out.println("before");
        System.out.println(array[6]);
        System.out.println("after");
    }

执行结果:
在这里插入图片描述

2.利用try-catch 手动处理异常

利用try -catch 捕获程序可能出现的异常,程序会依然执行,但是不执行发生异常处的代码。
//利用try-catch 处理异常

    public static void main2(String[] args) {
        int[] array = {1, 2, 3, 4};
        try {
            System.out.println("before");
            System.out.println(array[6]);
            System.out.println("after");
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }

执行结果:
在这里插入图片描述

3.利用了try-catch 捕获异常

但是没有捕获到程序可能发生的异常,则交由JVM处理,程序就会终止。

 //利用try catch 但是未捕获到
    public static void main3(String[] args) {
        int[] array = {1, 2, 3, 4};
        try {
            System.out.println("before");
            System.out.println(array[6]);
            System.out.println("after");
        } catch (InputMismatchException e) {
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }

执行结果:
在这里插入图片描述

  • 可利用try catch 捕获多个异常
  • 一般在catch中捕获特定的异常,不建议直接捕获父类的异常
4.在finally类中完成善后工作
  public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4};
        try {
            System.out.println("before");
            System.out.println(array[6]);
            System.out.println("after");
        } catch (InputMismatchException e) {
            e.printStackTrace();
        } finally {
            System.out.println("after try catch");
        }
    }

执行结果:
在这里插入图片描述
finally块的代码无论是否发生异常一定会被执行,并且在最后执行。

  • finally一般用于资源的关闭。
5.可利用try回收资源

将Scanner放入到try后的()中,则不需关闭
代码示例:

 public static void main6(String[] args) {
        try(Scanner scanner = new Scanner(System.in){
            int a = scanner.nextInt();
            System.out.println("a =" + a );
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
6. 抛出异常

手动抛出异常,throw->特定的异常,一般抛出自定义的异常。
throws 一般放在方法得到后面,声明方法可能抛出异常,在调用此方法时需要使用try- catch进行捕获。

排查错误信息:
出现多个异常,找最高的异常,则找栈顶的异常
在IDEA中异常有多个灰色,以及蓝色,我们需要找蓝色的异常

3.异常当中的注意问题:return

finally 块中的代码保证程序一定会执行到,但是遇见return时会出现问题。

  public static int func() {
        Scanner scanner = new Scanner(System.in);
        try{
            return scanner.nextInt();
        } catch (InputMismatchException e){
            return 20;
        } finally {
            return 30;
        }
    }
    public static void main7(String[] args) {
        System.out.println(func());
    }

在这个代码中无论怎样输入,返回的永远是30

  • finally 执行的时机是在方法返回之前(try 或者 catch 中如果有 return 会在这个 return 之前执行 finally).
  • 但如果 finally 中也存在 return 语句, 那么就会执行 finally 中的 return, 从而不会执行到 try 中原有的 return.
  • 建议不要在finally当中写return.finally当中的return会抑制try 和catch当中的return。
  • 在finally当中也不能抛出异常
  • 在finaliy当中只关闭资源。

4.自定义异常

在实际场景中我们可根据需求创建异常

  • 自定义异常需要继承异常父类Exception
class MyException extends Exception {
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值