Java:异常

1. 认识异常

我们在代码种肯定见过了一些异常,例如:

除数为0

System.out.println(10 / 0);
// 执行结果
Exception in thread "main" java.lang.ArithmeticException: / by zero

数组下表越界

int[] arr = {1, 2, 3};
System.out.println(arr[100]);
// 执行结果
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

2. 异常的基本用法

2.1 捕获异常

语法

try{ 
 有可能出现异常的语句 ; 
}catch (异常类型 异常对象) {
} ... 
finally {
 异常的出口
}
  • try代码块种放的是可能出现异常的代码
  • catch代码块种放的是出现异常后的处理行为
  • finally 代码块中的代码用于处理善后工作, 会在最后执行
  • catch和finally都可以根据情况选择加或不加

示例1:不处理异常

int[] arr = {1, 2, 3};
System.out.println("before");
System.out.println(arr[100]);
System.out.println("after");
// 执行结果
before
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100

程序在碰到异常后就终止,并没有执行打印after.

使用try catch

int[] arr = {1, 2, 3};
try {
    System.out.println("before");
    System.out.println(arr[100]);
    System.out.println("after");
} catch (ArrayIndexOutOfBoundsException e) {
    // 打印出现异常的调用栈
    e.printStackTrace();
} finall{
	System.out.println("after try catch");
}

// 执行结果
before
java.lang.ArrayIndexOutOfBoundsException: 100
 at demo02.Test.main(Test.java:10)
after try catch

当try种出现异常,那么try代码块中的程序就不会继续执行,而是交给catch中的代码来执行,catch执行完毕会继续往下执行.

示例2: catch只能处理对应种类的异常

 int[] arr = {1,3,5};
        try{
            System.out.println("before");
            System.out.println(arr[10]);
            System.out.println("after");
        }catch (NullPointerException e){
            System.out.println("数组越界");
            e.printStackTrace();
        }finally {
            System.out.println("after try catch");
        }
        //打印结果
        before
after try catch
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
	at demo.Demo.main(Demo.java:11)

示例3: catch可以有多个

 int[] arr = {1,3,5};
        try{
            System.out.println("before");
            System.out.println(arr[10]);
            System.out.println("after");
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("数组下表越界");
            e.printStackTrace();
        } catch (NullPointerException e){
            System.out.println("空指针异常");
            e.printStackTrace();
        }finally {
            System.out.println("after try catch");
        }
//打印结果
before
数组下表越界
after try catch
java.lang.ArrayIndexOutOfBoundsException: 10
	at demo.Demo.main(Demo.java:11)

一段代码可能会抛出多种不同的异常, 不同的异常有不同的处理方式. 因此可以搭配多个 catch 代码块. 如果多个异常的处理方式是完全相同, 也可以写成这样.

catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
 ...
}

Exception类是所有异常类的父类,可以用这个类表示捕获所有异常,但不推荐使用.

catch (Exception e) {
 ...
}

finally表示最后的善后工作,例如可以释放资源.

2.2 异常处理流程

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

2.3 抛出异常

除了Java内置的类会抛出一些异常之外,我们还可以手动抛出某个异常,使用throw关键字完成这个操作

public static void main(String[] args) { 
 System.out.println(divide(10, 0)); 
} 
public static int divide(int x, int y) { 
 if (y == 0) { 
 throw new ArithmeticException("抛出除 0 异常"); 
 } 
 return x / y; 
} 
// 执行结果
Exception in thread "main" java.lang.ArithmeticException: 抛出除 0 异常
 at demo02.Test.divide(Test.java:14) 
 at demo02.Test.main(Test.java:9) 

在这段代码中,我们可以根据实际情况来抛出需要的异常,在构造异常对象同时可以指定一些描述性信息.

异常说明

我们在处理异常的时候,通常希望知道这段代码中究竟会出现哪些可能的异常,可以使用throws关键字,把可能抛出的异常显示的标注在方法定义的位置,从而提醒调用者注意捕获这些异常.

public static int divide(int x, int y) throws ArithmeticException { 
 if (y == 0) { 
 throw new ArithmeticException("抛出除 0 异常"); 
 } 
 return x / y; 
} 

3. Java异常体系

在这里插入图片描述

  • 顶层类 Throwable 派生出两个重要的子类, Error 和 Exception
  • 其中 Exception 有一个子类称为 RuntimeException , 这里面又派生出很多我们常见的异常类 NullPointerException, IndexOutOfBoundsException等

Java中将Error的子类或RuntimeException的子类的所有异常称为非受查异常,其他的异常称为受查异常.

当一段代码可能抛出受查异常时,那么必须显式进行处理,显示处理方式有:

  1. 使用try catch包裹起来
  2. 在方法上加上异常说明,相当于将处理动作交给上级调用者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值