1.异常的定义
1.程序运行时,发生的不可预料或不被期望的事件,它阻止了程序按照预期的正常执行。
2.Java的异常可以分为Exception异常和Error错误。
3.Exception异常是程序运行时发生了不可预期的事件,也是我们异常处理的核心。
4.Error错误往往很严重,大部分情况无法使用代码去修复此类错误。
5.异常最先发生的地方,叫做异常抛出点。
6.在发生异常并处理完成后,返回到异常抛出点执行的方式称为恢复式异常处理。
7.在发生异常并处理完成后,从异常捕获代码后方继续执行的方式称为终结式异常处理。
2.RuntimeException
1.RuntimeException 是一种允许在编译时通过的一种错误(非静态错误),但是在运行期间可能会出现的一些异常。
2.常见的CheckedException异常:
NullPointerException,ClassCastException,IndexOutOfBoundsException
public static void main(String[] args){
Student student1=new Student("001","蔡蔡",20);
ArrayList<Student> studentList=new ArrayList<Student>();
studentList.add(student1);
//这步操作就会引发空指针异常
//studentList=null;
//打印索引为1的学生的姓名,会打印数组下标越界的错误
System.out.println(studentList.get(1).getName());
}
3.CheckedException
1.CheckedException是一种默认可以被修复的异常,如果程序没有处理checked异常,那么程序在编译时出现无法编译通过的情况。Java文件------(编译)------>class文件
2.程序运行前就需要我们处理之后,再去运行代码
3.常见的CheckedException异常:
SQLException,IOException,ClassNotFoundException,NoSuchMethodException
4.异常的处理方式
1.Java中的异常处理对面向于RuntimeException,CheckedException两大异常
2.通过代码去处理,解决我们的异常行为(面向的是可修复的问题)
3.通过try-catch-finally来捕获并处理异常
4.通过throws关键字抛出异常
5.通过throw抛出异常
5.try-catch-finally的书写格式
try{
可能发生异常的代码
}catch(异常类 自定义的对应异常类名称){
发生异常时的处理语句
}catch(异常类 自定义的对应异常类名称){
发生异常时的处理语句
}
...
finally{
finally为可选操作,无论是否发生异常,都会执行的代码块
}
public static void main(String[] args){
Student student1=new Student("001","蔡蔡",20);
ArrayList<Student> studentList=new ArrayList<Student>();
studentList.add(student1);
String studentName=null;
try {
studentName=studentList.get(1).getName();
} catch (NullPointerException e) {
System.out.println("发生了空指针异常");
// 打印异常信息
e.printStackTrace();
}catch (IndexOutOfBoundsException e) {
System.out.println("发生了索引越界异常");
// 打印异常信息
e.printStackTrace();
}finally{
System.out.println("对于代码的异常捕获已经结束");
}
System.out.println(studentName);
}
6.try-catch-finally的注意点
1.catch语句中的异常只会匹配一次,并且由从上到下的顺序去匹配
2.优先发生的异常会被优先捕获
3.子异常类应该放置在其父异常的前方,避免每次直接匹配父类异常,导致子类异常声明无效。
Exception异常类是父类,NullPointerException,IndexOutOfBoundsException是子类。
7.throws关键字抛出异常
1.在可能发生异常的代码块中,通过throws关键抛出异常。(不提出解决方案)
2.一般直接跟在方法名称后即可。
3.格式: 权限修饰符 其他修饰符 方法返回体 方法名称() throws 异常名称1,异常名称2,...{ }
public static void main(String[] args){
int result=0;
result=calculation(2, 0);
System.out.println(result);
}
public static int calculation(int x,int y) throws ArithmeticException{
return x/y;
}
8.throw关键字抛出异常
1.throw出现在方法体中,用来抛出一个异常,程序会在throw语句后停止,如果存在catch语句,则会执行catch中的代码块。
2.throw和throws不同之处:throw是一个执行语句,如果我们想要让它抛出什么样的错误,他就会直接抛出。而throws是会根据代码的执行结果去返回对应的异常。
3.格式:
throw new 异常名称(例:exception);
例如:throw new NullPointerException();
public static void main(String[] args){
int result=0;
try {
result=calculation(2, 0);
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("更新result的值时发生了错误");
}
System.out.println("该方法的结果是:"+result);
}
public static int calculation(int x,int y) {
if (y==0) {
throw new ArithmeticException();
}
return x/y;
}
注意:也可以throw一个空指针异常
public static void main(String[] args){
int result=0;
try {
result=calculation(2, 0);
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("更新result的值时发生了错误");
}
System.out.println("该方法的结果是:"+result);
}
public static int calculation(int x,int y) {
if (y==0) {
//throw new ArithmeticException();
throw new NullPointerException();
}
return x/y;
}
9.使用捕获异常的注意点:
1.不是所有异常或者错误我们都可以通过捕获或者抛出的形式解决。
比如error错误,可能是JVM错误或者计算机错误,不单纯是代码问题。
2.不必滥用异常的捕获。
比如不确定的代码加异常捕获,第三方接口(调用同事的接口),加个catch,通过日志打印错误信息。