一:什么是异常
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
访问 null 对象
public class Test {
public int num = 10;
public static void main(String[] args) {
Test t = null;
System.out.println(t.num);
}
}
// 执行结果
Exception in thread "main" java.lang.NullPointerException
以上是几个常见的异常类型,总结一下:异常指的就是程序在 运行时 出现错误时通知调用者的一种机制.这里注意一下是运行时,指的是已经编译过后了。
2防御式编程
错误在代码中是客观存在的
.
因此我们要让程序出现问题的时候及时通知程序猿
.
我们有两种主要的方式
LBYL
: Look Before You Leap.
在操作之前就做充分的检查
.
EAFP
: It's Easier to Ask Forgiveness than Permission. "
事后获取原谅比事前获取许可更容易
".
也就是先操作
,
遇到
问题再处理
.
二:如何处理异常
1 捕获异常
基本用法
try{
有可能出现异常的语句 ;
}[catch (异常类型 异常对象) {
} ... ]
[finally {
异常的出口
}]
try
代码块中放的是可能出现异常的代码
.
catch
代码块中放的是出现异常后的处理行为
.
fifinally
代码块中的代码用于处理善后工作
,
会在最后执行
.
其中
catch
和
fifinally
都可以根据情况选择加或者不加。
代码示例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 没有正确输出。
代码示例
2
使用
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();
}
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
执
行完毕会继续往下执行
.
代码示例
3
catch
只能处理对应种类的异常
我们修改了代码
,
让代码抛出的是空指针异常。
int[] arr = {1, 2, 3};
try {
System.out.println("before");
arr = null;
System.out.println(arr[100]);
System.out.println("after");
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
System.out.println("after try catch");
// 执行结果
before
Exception in thread "main" java.lang.NullPointerException
at demo02.Test.main(Test.java:11)
此时
, catch
语句不能捕获到刚才的空指针异常
.
因为异常类型不匹配,由此得知,catch的异常必须与抛出的异常对应才能顺利捕获。
代码示例4
fifinally
表示最后的善后工作
,
例如释放资源:
int[] arr = {1, 2, 3};
try {
System.out.println("before");
arr = null;
System.out.println(arr[100]);
System.out.println("after");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finally code");
}
// 执行结果
before
java.lang.NullPointerException
at demo02.Test.main(Test.java:12)
finally code
无论是否存在异常
, fifinally
中的代码一定都会执行到
.
保证最终一定会执行到
Scanner
的
close
方法。
代码示例5
使用
try
负责回收资源
将
Scanner
对象在
try
的
( )
中创建
,
就能保证在
try
执行完毕后自动调用
Scanner
的
close
方法。
try (Scanner sc = new Scanner(System.in)) {
int num = sc.nextInt();
System.out.println("num = " + num);
} catch (Exception e) {
e.printStackTrace();
}
2 异常处理流程
程序先执行
try
中的代码
如果
try
中的代码出现异常
,
就会结束
try
中的代码
,
看和
catch
中的异常类型是否匹配
.
如果找到匹配的异常类型
,
就会执行
catch
中的代码
如果没有找到匹配的异常类型
,
就会将异常向上传递到上层调用者
.
无论是否找到匹配的异常类型
, fifinally
中的代码都会被执行到
(
在该方法结束之前执行
).
如果上层调用者也没有处理的了异常
,
就继续向上传递
.
一直到
main
方法也没有合适的代码处理异常
,
就会交给
JVM
来进行处理
,
此时程序就会异常终止。
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;
}
注:水平有限,如有不当之处请不吝赐教,在此深表感谢,愿自己越来越好。hold on!