1.掌握异常概述
2.处理异常
3.自定义异常
4.掌握异常处理注意事项
一、异常概述
1.1:什么是异常?
-
异常?Java代码在编译、运行时期发生的问题就是异常。
-
Java如何表示异常(面向对象)
-
在Java中,把异常信息封装成一个类。当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如一场出现的位置、原因等)
-
用类封装
-
1.2:异常继承体系
-
因为非正常现象太多
-
Exception的直接子类:编译时异常(要求程序员在编写程序阶段必须对这些异常进行处理,如果不处理编译器报错,因此得名编译时异常)
-
RuntimeExcception:运行时异常(在编写程序阶段可以预先处理,也可以不管,都行)
-
Java,就会出现报错
-
1.3:异常和错误的区别
-
异常:指程序在编译、运行期间发生了某种异常(XxxException),我们可以对异常进行具体要的处理。若不处理异常,程序将会结束运行。
异常的产生演示如下:
public class Demo { public static void main(String[] args) { int[] arr = new int[3]; System.out.println(arr[0]); System.out.println(arr[3]); ///因为上一行代码发生了数组越界ArrayIndexOutOfBoundsException // 由于没有处理异常,所以下一行代码不会执行 System.out.println("over"); } }
-
错误:指程序员在运行期间发生了某种错误(XxxError),Error错误通常没有具体的处理方式,程序将会结束运行,Error错误的发生期间都是系统级别的问题,都是jvm所在系统发生的,并反馈给jvm的。我们无法针对处理,只能修正代码。
-
比异常严重,因为无法处理
-
-
错误的产生演示如下:
public class Demo { public static void main(String[] args) { int[] arr = new int[3000*3000*3000]; //超出数组可承受数量
1.4:异常的产生过程解析
-
先运行下面的程序,程序会产生一个数组索引越界异常ArrayIndexOfBoundsException。我们来通过图解来解析下异常的产生过程:
-
工具类
-
class ArrayTools{ //对给定的数组通过给定的角标获取元素。 public static int getElement(int[] arr,int index) { int element = arr[index]; return element; } }
-
测试类
class ExceptionDemo2 { public static void main(String[] args) { int[] arr = {34,12,67}; int num = ArrayTools.getElement(arr,4) System.out.println("num="+num); System.out.println("over"); } }
1.5如何查看异常
二、异常处理
2.1:抛出异常:throw
-
在定义方法时,方法需要接受参数。那么,当调用方法使用接受到的参数时,首先需要先对参数数据进行合法的判断,数据若不合法,就应该告诉调用者,传递合法的数据进来。这时需要使用抛出异常(throw)的方式来告诉调用者。
-
在java中,提供了一个throw关键字,它用来抛出一个指定的异常对象。那么,抛出一个异常具体如何操作呢?
-
-
创建一个异常对象。封装一些提示信息(信息可以自己编写)。
-
-
-
需要将这个异常对象告知给调用者。怎么告知呢?怎么将这个异常对象传递到调用者处呢?通过关键字throw就可以完成。throw 异常对象;
-
-
throw用在方法内,用来抛出一个异常对象,将这个异常对象传递到调用者处,并结束当前方法的执行。
-
提前告知调用者,避免该情况发生
-
使用格式:
throw new 异常类名(参数);
-
代码实例:
-
工具类
package 异常; import java.sql.SQLException; public class Demo1 { public static void getData(int arr[],int index){ int intdex = arr.length; if (!(0<= index && index <arr.length)){ throw new ArrayIndexOutOfBoundsException("对不起,请您写一个合法的索引"); } System.out.println(arr[index]); } }
-
测试类
package 异常; public class Demo { public static void main(String[] args) { int[] arr = new int[3]; Demo1.getData(arr,2); } }
2.2下面是异常ArrayIndexOutOfBoundsException与NullPointerException的构造方法
-
学习完抛出异常的格式后,我们通过下面程序演示下throw的使用。
-
-
编写工具类,提供获取数组指定索引处的元素值
-
工具类
package 异常测试; public class tool { public static int getData(int arr[],int index){ if (arr==null){ throw new NullPointerException("指针指向为空"); } if(index<0 || index>=arr.length){ throw new ArrayIndexOutOfBoundsException("错误的角标,"+index+"索引在数组中不存在"); } int element = arr[index]; return element; } }
测试类
package 异常测试; public class Demo { public static void main(String[] args) { int[] arr = new int[3]; int data = tool.getData(null, 2);// 调用方法,获取数组中指定索引处元素 int data1 = tool.getData(arr, 5);// 调用方法,获取数组中指定索引处元素 System.out.println(data);//打印获取到的元素值 } }
2.3:声明异常throws
-
声明:将问题标识出来,报告给调用者。
-
-
如果方法内通过throw抛出了编译时异常,而没有捕获处理(稍后讲解该方式),那么必须通过throws进行声明,让调用者去处理。
-
如果调用者,调用了该方法,A:没有捕获;B:必须使用throws
-
-
声明异常格式:修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2…{ }
-
代码实例
-
-
工具类
-
package 声明异常08; public class ArraysTool { public static void getData(int[] arr,int index)throws ArrayIndexOutOfBoundsException{ //int length = arr.length; /*if (!(0<= index && index <length)){ //throw new ArrayIndexOutOfBoundsException("对不起,请您写一个合法的索引"); throw new ArrayIndexOutOfBoundsException(index); }*/ // System.out.println(arr[index]); System.out.println(arr[index]); } }
-
测试类throws用于进行异常类的声明,若该方法可能有多种异常情况产生,那么在throws后面可以写多个异常类,用逗号隔开
package 声明异常08; public class Demo { public static void main(String[] args)throws ArrayIndexOutOfBoundsException{ int[] arr = {12,22,33}; ArraysTool.getData(arr,12); } }
-
代码实例
public static void getData(int[] arr,int index)throws ArrayIndexOutOfBoundsException,NullPointerException{
2.4:throw和throws区别
-
thows用在方法声明的时候,表示这个方法可能会抛出这个异常,可以声明多种异常,由方法的调用者捕获
-
thow用在方法体内,用于抛出一个异常实例,由方法内部的语句进行处理。
-
throw和throws配合使用
-
-
因为有throw,必须要有throws(编译时 异常)
-
如果是运行时异常,两者可一个即可
-
-
类的角度分类
-
-
运行时异常类:RuntimeException类以及相关的子类
-
编译时异常类:非RuntimeException类以及相关子类
-
-
-
2.5捕获异常try...catch...finally
-
捕获:Java中对异常有针对性的语句进行捕获,可以对出现的异常进行指定方式的处理
-
格式
try { //需要被检测的语句。 } catch(异常类 变量) { //参数。 //异常的处理语句。 } finally { //一定会被执行的语句。 }
-
try:该代码块中编写可能产生异常的代码。
-
catch:用来进行某种异常的捕获,实现对捕获到的异常进行处理。
-
finally:有一些特定的代码无论异常是否发生,都需要执行。另外,因为异常会引发程序跳转,导致有些语句执行不到。而finally就是解决这个问题的,在finally代码块中存放的代码都是一定会被执行的。
class ExceptionDemo{ public static void main(String[] args){ //throws ArrayIndexOutOfBoundsException try { int[] arr = new int[3]; System.out.println( arr[5] );// 会抛出ArrayIndexOutOfBoundsException 当产生异常时,必须有处理方式。要么捕获,要么声明。 } catch (ArrayIndexOutOfBoundsException e) { //括号中需要定义什么呢?try中抛出的是什么异常,在括号中就定义什么异常类型。 System.out.println("异常发生了"); } finally { arr = null; //把数组指向null,通过垃圾回收器,进行内存垃圾的清除 } System.out.println("程序运行结果"); } }
2.6:try…catch…finally异常
-
try catch finally组合:检测异常,并传递给catch处理,并在finally中进行资源释放。
-
try catch组合 : 对代码进行异常检测,并对检测的异常传递给catch处理。对异常进行捕获处理。
void show(){ //不用throws try{ throw new Exception();//产生异常,直接捕获处理 }catch(Exceptione){ //处理方式 } }
-
一个try 多个catch组合 : 对代码进行异常检测,并对检测的异常传递给catch处理。对每种异常信息进行不同的捕获处理
void show(){ //不用throws try{ throw new Exception();//产生异常,直接捕获处理 }catch(XxxExceptione){ //处理方式 }catch(YyyExceptione){ //处理方式 }catch(ZzzExceptione){ //处理方式 } }
-
注意:这种异常处理方式,要求多个catch中的异常不能相同,并且若catch中的多个异常之间有子父类异常的关系,那么子类异常要求在上面的catch处理,父类异常在下面的catch处理。
-
-
try finally 组合: 对代码进行异常检测,检测到异常后因为没有catch,所以一样会被默认jvm抛出。异常是没有捕获处理的。但是功能所开启资源需要进行关闭,所有finally。只为关闭资源。
-
void show(){//需要throws try{ throw new Exception(); }finally { //释放资源 } }
2.7:运行时异常
-
RuntimeException和他的所有子类异常,都属于运行时期异常
-
NullPointerException,ArrayIndexOutOfBoundsException等都属于运行时期异常.
-
运行时期异常的特点:
-
-
方法中抛出运行时期异常,方法定义中无需throws声明,调用者也无需处理此异常
-
运行时期异常一旦发生,需要程序人员修改源代码.
-