学习目标:
1、了解异常的嵌套的语法和作用
2、了解级联捕获异常的意义和语法
学习过程:
这节我们需要探讨的问题是,如果我们需要try尝试捕获的异常不止一个,那么我们需要如何处理呢?
一、分开捕获或者嵌套使用
我们先看看下面这段代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class Cal { public int div( int a, int b) { int result = a / b; return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int s = 0 ; int num1 = 0 ; int num2 = 0 ; //1、这里可能会抛出异常 System.out.print( "num1=" ); num1 = scanner.nextInt(); System.out.print( "num2=" ); num2 = scanner.nextInt(); Cal cal = new Cal(); //2、这里也可能抛出异常 s = cal.div(num1, num2); System.out.println(s); } } |
在这段代码中有可能抛出异常的有两个地方,那么我们应该如何处理呢。
1、当然我们可以分开捕获。如下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class Cal { public int div( int a, int b) { int result = a / b; return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int s = 0 ; int num1 = 0 ; int num2 = 0 ; //1、这里可能会抛出异常 try { System.out.print( "num1=" ); num1 = scanner.nextInt(); System.out.print( "num2=" ); num2 = scanner.nextInt(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Cal cal = new Cal(); //2、这里也可能抛出异常 try { s = cal.div(num1, num2); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(s); } } |
2、我们还可以在try里面嵌套的使用try语句。如下面代码所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public class Cal { public int div( int a, int b) { int result = a / b; return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int s = 0 ; int num1 = 0 ; int num2 = 0 ; try { //1、这里可能会抛出异常 System.out.print( "num1=" ); num1 = scanner.nextInt(); System.out.print( "num2=" ); num2 = scanner.nextInt(); try { Cal cal = new Cal(); //2、这里也可能抛出异常 s = cal.div(num1, num2); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(s); } } |
二、使用级联捕获异常
上面介绍的这两种方法都不是好办法,以为过多的使用try捕获异常会影响程序的效率。所以我们推荐使用的是级联捕获异常。格式如下
try{
…...
}catch(ArrayIndexOutOfBoundsException e) {
……
} catch(ArithmeticException e) {
……
} catch(Exception e) {
……
}
注意:使用多重 catch 语句时,异常子类一定要位于异常父类之前。
所以以下这种方式是错误的。
try{
…...
} catch(Exception e) {
……
} catch(ArrayIndexOutOfBoundsException e) {
……
}
好,那么我们可以修改上面的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public class Cal { public int div( int a, int b) { int result = a / b; return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int s = 0 ; int num1 = 0 ; int num2 = 0 ; try { //1、这里可能会抛出异常 System.out.print( "num1=" ); num1 = scanner.nextInt(); System.out.print( "num2=" ); num2 = scanner.nextInt(); Cal cal = new Cal(); //2、这里也可能抛出异常 s = cal.div(num1, num2); } catch (ArithmeticException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InputMismatchException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } System.out.println(s); } } |
由于多次的使用try或影响效率。所以我们如果碰到循环的时候,应该把try语句放到循环的外面,例如我们并不推荐你这样写代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void main(String[] args) { int [] arr = { 1 , 2 , 3 , 4 }; Cal cal = new Cal(); for ( int i = 0 ; i < arr.length; i++) { try { int s = cal.div(arr[i], 2 ); } catch (Exception e) { // TODO: handle exception } } } |
你可以修改成为这样:
1 2 3 4 5 6 7 8 9 10 11 |
public static void main(String[] args) { int [] arr = { 1 , 2 , 3 , 4 }; Cal cal = new Cal(); try { for ( int i = 0 ; i < arr.length; i++) { int s = cal.div(arr[i], 2 ); } } catch (Exception e) { // TODO: handle exception } } |