1、异常处理类的结构体系
1.程序出现异常是正常现象【就像人会生病一样】
2.Java中异常类Throwable[类]顶级异常类。
3.Throwable[类]有两个子类:1.Error(错误) 2.Exception(异常)
Error是java程序运行中不可预料的异常情况,这种异常发生以后,会直接导致JVM不可处理或者不可恢复的情况。所以这种异常不可能抓取到,比如OutOfMemoryErrorNoClassDefFoundError等。【癌症】
Exception是java程序运行中可预料的异常情况,咱们可以获取到这种异常,并且对这种异常进行业务外的处理。【感冒】
4.Exception被分为运行时异常与非运行时异常
运行时异常--非检查性异常 在代码编写时,可以忽略捕获操作(比如:ArrayIndexOutOfBoundsException),这种异常是在代码编写或者使用过程中通过规范可以避免发生的。
非运行时异常--检查性异常 必须在编写代码时,使用try catch捕获(比如:IOException异常)。
2、Java异常处理的原理
1.异常可以自己处理掉
通过try-catch代码块处理异常。
try-catch代码块处理步骤:
将可能出现异常的java代码使用”try{异常可能发生的代码}catch(要抓捕的异常){异常处理方式}”块包裹,如果出现异常try{}就会捕获异常,终止代码的运行,将捕获的异常交给catch(){}去处理异常。
例如:
public class TestException {
public static void main(String[] args) {
int num1=10;
int num2=0;
int num3=num1/num2;
System.out.println("num3=="+num3);
}
}
java.lang.ArithmeticException---除数不能为0的异常
try-catch代码块
格式:
try{
异常可能发生的代码
}catch(要抓捕的异常类型){
异常处理方式
}
1.try{}--捕获可能发生的异常
2.catch(//要抓捕的异常){//异常处理方式}---抓获处理异常
catch后面的“()”中一般要定义为具体异常类型。
具体异常类型不明确的时候可以使用Exception/Throwable
catch的{}--- 具体异常的处理过程。往往都是打印堆栈到控制台,查看具体情况,修改程序去避免
3.一个try{}后面可以跟随多个catch(要抓捕的异常){异常处理方式},多个catch时要抓捕的异常类型需要按照由小到大的顺序排列。
4.【finally{}】--出现在catch的“{}”后面,可以写也可以不写。
有无异常都要执行的动作。
当有一定需要执行的代码时写道finally中。
注:finally语句是在return语句之前执行的
2.异常不自己处理掉
如果出现异常,java会根据问题所描述的异常类,创建一个对象(实例)具体异常对象,然后将该对象抛给上一级【谁调用谁就是上一级】
throws--声明方法抛出异常给上一级【谁调用谁就是上一级】
具体步骤:
method具体出异常处 --》main主方法--》jvm虚拟机--》将异常出现的位置和原因打印在控制台
package com.wangxing.test2;
public class TestClass {
public int getInt()throws Exception{
int a=10;
int b=0;
int c=a/b;
return c;
}
}
package com.wangxing.test2;
public class Main {
public static void main(String[] args) throws Exception{
TestClass tc=new TestClass();
int res=tc.getInt();
System.out.println("res=="+res);
}
}
控制台
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.wangxing.test2.TestClass.getInt(TestClass.java:8)
at com.wangxing.test2.Main.main(Main.java:7)
3.throw关键字与自定义异常
简单的自定义异常--编写一个新类,继承Throwable/Exception/RumtimeException,在构造中访问父类的构造方法
throw---手动引发一个具体异常
package com.wangxing.test3;
//继承Throwable/Exception/RumtimeException,在构造中访问父类的构造方法
public class MyException extends Exception{
public MyException(String info){
super(info);
}
}
package com.wangxing.test3;
public class MyArray {
private int size=0;
public MyArray(int size)throws MyException{
if(size>0){
this.size=size;
}else{
//手动引发一个异常
throw new MyException("数组大小不能为负数!");
}
}
public int[] createIntArray(){
int arr[]=new int[this.size];
return arr;
}
}
package com.wangxing.test3;
public class Main {
public static void main(String[] args){
try{
MyArray ma=new MyArray(5);
}catch(MyException e){
e.printStackTrace();
}
}
}
java中的常见运行时异常
1.NullPointerException - 空指针引用异常(对象无内容只有null时,会抛出该异常)
2.ClassCastException - 类型强制转换异常。
3.IllegalArgumentException - 传递非法参数异常。
4.ArithmeticException - 算术运算异常
5.ArrayStoreException - 向数组中存放与声明类型不兼容对象异常
6.IndexOutOfBoundsException - 下标越界异常
7.NegativeArraySizeException - 创建一个大小为负数的数组错误异常
8.NumberFormatException - 数字格式异常
9.SecurityException - 安全异常
10.UnsupportedOperationException - 不支持的操作异常
总结:1.如果有异常就try{}catch(){},如果不想try{}catch(){}那么就声明方法throws异常。
2.没有特殊要求的时候我们都是打印堆栈异常,查看程序的具体异常和位置,方便修改。
3.我们轻易不会自己定义异常,因为java提供的异常类型已经足够使用。