索引
A:异常类的层次
B:异常类的常用方法
C:基础功能及注意事项代码实现
D:多重catch块(积极处理异常的方式)(try—catch处理方式为积极处理异常的方式)
E:finally块
A:异常类的层次是什么样子?
![](https://img-blog.csdnimg.cn/20200429001809711.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1Njk2Mjg4,size_16,color_FFFFFF,t_70)
注意,Object是所有的类的总类,并不是异常类的首类,那谁是首类?对,就是Throwable是首类。
B:异常类的常用方法
![](https://img-blog.csdnimg.cn/20200429081154937.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1Njk2Mjg4,size_16,color_FFFFFF,t_70)
C:基础功能及注意事项代码实现!
注意一:
在处理异常使用try语句块的时候,try语句块不能够单独使用,必须是try-catch连接或者try-finally连接,或者try-catch-finally;
注意二:
当try语句块找到发生异常的代码,异常代码后面的代码再不会执行。
注意三:
首先在try语句块中寻找是否有出现异常的代码,如果存在异常,就一个一个紧接着去找catch语句块,找到符合代码异常类型的catch语句块,再执行catch语句块中的语句体。如果try语句块中没有发现异常的话,catch语句块也不会执行。
注意四:
try语句块与catch语句块连接在一起的时候是紧连着的,中间不能添加任何代码。
注意五:
一个代码可以发生多个异常。
现在,看一下代码如何来处理?
package yiChang;
public class ChuLiYiChang2 {
public static void main(String[] args) {
try {
int result = divied(9, 0);// ArithmeticException ae = new ArithmeticException(); throw ae;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");// e=ae
e.printStackTrace();// 能够最全面的将Java异常信息的提示输出来
// System.out.println(e.getMessage()); 这个语句也能输出异常信息
// System.out.println(e.toString()); 这个语句也能输出异常信息
} catch (ArrayIndexOutOfBoundsException e) {// e=ab
System.out.println("下标越界了");
} catch (Exception e) {
System.out.println("其他异常");
}
System.out.println("end");
}
public static int divied(int a, int b) throws Exception {// 消极的处理异常方式,直接抛出异常
return a / b;
}
}
现在进行一个实例练习:
阅读以下程序代码块,分析程序可能产生的异常情况,捕捉并处理异常。
package yiChang;
public class ChuLiYiChang3 {
int[] a = { 1, 2, 6, 4, 0, 9 };
public void exceptest(int j) {
for (int i = 0; i < j; i++)
System.out.println("a[" + i + "]=" + a[i]);
}
public static void main(String[] args) {
ChuLiYiChang3 yc = new ChuLiYiChang3();
yc.exceptest(8);
}
}
上面的代码的异常发生的为《数组下标越界异常》
处理异常:
package yiChang;
public class ChuLiYiChang {
public static void main(String[] args) {
try {
int[] a = { 1, 3, 5, 7, 8 };
// ArrayIndexOutOfBoundsException ab = new ArrayIndexOutOfBoundsException();
// throw ab;
System.out.println(a[5]);// 当try块找到异常的语句后,try块后面的代码不会执行,所以9/0的异常也不会执行
int result = divied(9, 0);// ArithmeticException ae = new ArithmeticException(); throw ae;
} catch (ArithmeticException e) {
System.out.println("除数不能为0");// e=ae
e.printStackTrace();// 能够最全面的将Java异常信息的提示输出来
// System.out.println(e.getMessage()); 这个语句也能输出异常信息
// System.out.println(e.toString()); 这个语句也能输出异常信息
} catch (ArrayIndexOutOfBoundsException e) {// e=ab
System.out.println("下标越界了");
} catch (Exception e) {
System.out.println("其他异常");
}
System.out.println("end");
}
public static int divied(int a, int b) {
return a / b;
}
}
D:多重catch块
该机制与switch机制相同
一段代码可能会产生多个异常;当引发异常时,按照顺序查看每个catch语句,并执行第一个类型与异常类型匹配的语句;执行其中的一条catch语句之后,其他的catch语句将被忽略。
使用多重catch语句时,异常子类一定要位于异常父类的前面
即:写多个catch块的时候按照先子类后父类写,先具体再抽象写
例如:
try{
…
}catch(ArraylndexOutOfBoundsException e){ //子类 —具体
…
}catch(Exception e){//父类 —抽象
…
}
注意:一个try对应一个catch,一个try块出现异常不会影响其他try块代码内容的运行
例如:
package yiChang;
public class ChuLiYiChang4 {
public static void main(String[] args) {
try {
int []a= {1,2,4,6,7};
System.out.println(a[5]);//下标越界了
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界了哦");
e.printStackTrace();
}try {
int result = divied(9,3);
System.out.println(result);
}catch(ArithmeticException e) {
System.out.println("除数不能为0哦");
e.printStackTrace();
}catch(Exception e) {
System.out.println("是其他的异常哦");
}
System.out.println("-----end-----");
}
public static int divied (int a,int b) {
return a/b;
}
}
E:finally块
执行流程:
finally块的作用:
finally子句可以保证,无论try块中是否抛出异常,finally块中的语句都会被执行到,这样可以对程序的状态做统一的管理。
finally语句对增强程序的健壮性非常重要。
try{
//要监控错误的代码块
}catch(Exception e){
//Exception e的异常处理程序
}finally{
//在try结束前执行的代码块
}
finally语句块是比较特殊的,就算catch块中有return语句,也先会执行finally语句块后再进行return返回。
下面看一个例子:
package yiChang;
public class ChuLiYiChang5 {
public static void main(String[] args) {
try {
int []a= {1,3,5,8,9};
System.out.println(a[5]);//此处为发生异常的代码
return;//加一个返回语句,如果上面的代码块没有异常的话才会执行这个return语句,而且会先执行finally语句块中的语句再进行返回
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("-----数组下标越界异常-----");
//return; //此处不能加上这个return语句,如果加上的话,下面的System.out.println("---ok---");语句就不会被执行,因为该语句一定不会被执行到所以就报错
}finally {
System.out.println("---end---");
}
System.out.println("---ok---");
}
}
但是在上面的语句中如果加上一个语句,那么finally语句块的内容也不会被执行到
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("-----数组下标越界异常-----");
//return; //此处不能加上这个return语句,如果加上的话,下面的System.out.println("---ok---");语句就不会被执行,因为该语句一定不会被执行到所以就报错
System.exit(0);//加上这条语句,finally中的代码也不会被执行
}finally {
try—catch—finally注意问题: