异常:就是指程序在运行时**出现错误时通知调用者的一种机制。
运行时指的是程序已编译通过得到了class文件,再由JVM执行过程中出现的错误。
防御式编程:
错误在代码中是客观存在的,因此我们要让程序出现问题的时候及时通知程序员。我们有两种方式:
**LYBY:**在操作之前就做足充分的检查
**EAFP:**先操作,遇到问题再处理
异常的基本用法:
try{
有可能出现异常的语句;
}[catch (异常类型 异常对象){
}…]
[finally{
异常的出口
}]
注意:1.try代码块中放的是可能出现异常的代码
2.catch代码块中放的是出现异常后的行为处理
3.finally代码块中的代码是用来处理善后工作,会在最后执行
4.其中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
at Exception.Test.main(Test.java:7)
我们可以发现一旦出现异常,程序就终止了,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 Exception.Test.main(Test.java:8)
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 Exception.Test.main(Test.java:9)
此时,catch语句不能捕获到刚才的空指针异常,因为异常类型不匹配。
代码示例4: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){
//低音出现异常的调用栈
System.out.println("这个是数组下标越界异常");
e.printStackTrace();
}catch (NullPointerException e){
System.out.println("这个是空指针异常");
e.printStackTrace();
}
System.out.println("after try catch");
//执行结果
before
这个是空指针异常
java.lang.NullPointerException
at Exception.Test.main(Test.java:9)
after try catch
代码示例5:也可以用一个catch捕获所有异常(不推荐使用)
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();
}
System.out.println("after try catch");
//执行结果
before
java.lang.NullPointerException
at Exception.Test.main(Test.java:9)
after try catch
由于Exception类是所有异常类的父类,因此可以用这个类表示捕获所有异常。
代码示例6:finally表示最后的善后工作,例如释放资源
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 Exception.Test.main(Test.java:9)
finally code
无论是否存在异常,finally中的代码一定都会执行到
代码示例7:使用try负责收回资源
上边的代码可以有一种等价方法,将Scanner对象在try的()中创建,就能保证try执行完毕后自动调用Scanner的close方法。
try (Scanner sc = new Scanner(System.in)) {
int num = sc.nextInt();
System.out.println("nem = " + num);
} catch (Exception e) {
e.printStackTrace();
}
代码示例8:
public static void main(String[] args) {
try{
func();
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
System.out.println("after try catch");
}
public static void func(){
int[] arr = {1,2,3};
System.out.println(arr[100]);
}
//执行结果
java.lang.ArrayIndexOutOfBoundsException: 100
at Exception.Test.func(Test.java:14)
at Exception.Test.main(Test.java:6)
after try catch
异常的处理流程:
1.程序先执行try中的代码
2.如果tryh中的代码出现异常,就会结束try中的代码,看和catch中的异常类型是否匹配;
3.如果找到匹配的异常类型,就会执行catch中的代码;
4.如果没有找到匹配的类型,就会将异常向上传递到上层调用者;
5.无论是否找到匹配的异常类型,finally中的代码都会执行到(在该方法结束之前执行)
6.如果上层调用者也没有处理的异常,就会继续向上传递
7.一直到main方法也没有合适的代码处理异常,就会交给JVM来处理,此时程序就会异常终止
关于finally的注意事项:
finally执行的实际是在方法返回之前(try或者catch中如果有return,会在return之前执行finally),但是如果finally中也存在return语句,那么就会执行finally中的return,从而不会执行到try中原有的return
(一般不建议在finally中写return语句)
抛出异常:
使用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 Exception.Test.divide(Test.java:9)
at Exception.Test.main(Test.java:5)
异常说明:
我们在处理异常时通常希望知道这段代码中究竟会出现哪些异常,就可以使用throws关键字,把可能抛出的异常显式的标注在方法定义的位置,从而提醒调用者捕获这些异常。
public static int divide(int x,int y)throws ArithmeticException{
if(y == 0){
throw new ArithmeticException("抛出0异常");
}
return x / y;
}