Java知多少(48)try语句的嵌套

Try语句可以被嵌套。也就是说,一个try语句可以在另一个try块内部。每次进入try语句,异常的前后关系都会被推入堆栈。如果一个内部的try语句不含特殊异常的catch处理程序,堆栈将弹出,下一个try语句的catch处理程序将检查是否与之匹配。这个过程将继续直到一个catch语句匹配成功,或者是直到所有的嵌套try语句被检查耗尽。如果没有catch语句匹配,Java的运行时系统将处理这个异常。下面是运用嵌套try语句的一个例子:

 1 // An example of nested try statements.
 2 class NestTry {
 3     public static void main(String args[]) {
 4         try {
 5             int a = args.length;
 6             /* If no command-line args are present,the following statement will generate a divide-by-zero exception. */
 7             int b = 42 / a;
 8             System.out.println("a = " + a);
 9             try { // nested try block
10                 /* If one command-line arg is used,then a divide-by-zero exception will be generated by the following code. */
11                 if(a==1) a = a/(a-a); // division by zero
12                 /* If two command-line args are used,then generate an out-of-bounds exception. */
13                 if(a==2) {
14                     int c[] = { 1 };
15                     c[42] = 99; // generate an out-of-bounds exception
16                 }
17             } catch(ArrayIndexOutOfBoundsException e) {
18                 System.out.println("Array index out-of-bounds: " + e);
19             }
20         } catch(ArithmeticException e) {
21             System.out.println("Divide by 0: " + e);
22         }
23     }
24 }

如你所见,该程序在一个try块中嵌套了另一个try块。程序工作如下:当你在没有命令行参数的情况下执行该程序,外面的try块将产生一个被零除的异常。程序在有一个命令行参数条件下执行,由嵌套的try块产生一个被零除的错误。因为内部的块不匹配这个异常,它将把异常传给外部的try块,在那里异常被处理。如果你在具有两个命令行参数的条件下执行该程序,由内部try块产生一个数组边界异常。下面的结果阐述了每一种情况:

C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a = 1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a = 2
Array index out-of-bounds: java.lang.ArrayIndexOutOfBoundsException

当有方法调用时,try语句的嵌套可以很隐蔽的发生。例如,你可以把对方法的调用放在一个try块中。在该方法内部,有另一个try语句。这种情况下,方法内部的try仍然是嵌套在外部调用该方法的try块中的。下面是前面例子的修改,嵌套的try块移到了方法nesttry( )的内部:

 1 /* Try statements can be implicitly nested via calls to methods. */
 2 class MethNestTry {
 3     static void nesttry(int a) {
 4         try { // nested try block
 5             /* If one command-line arg is used,then a divide-by-zero exception will be generated by the following code. */
 6             if(a==1) a = a/(a-a); // division by zero
 7             /* If two command-line args are used,then generate an out-of-bounds exception. */
 8             if(a==2) {
 9                 int c[] = { 1 };
10                 c[42] = 99; // generate an out-of-bounds exception
11             }
12         } catch(ArrayIndexOutOfBoundsException e) {
13             System.out.println("Array index out-of-bounds: " + e);
14         }
15     }
16 
17     public static void main(String args[]) {
18         try {
19             int a = args.length;
20            /* If no command-line args are present,the following statement will generate a divide-by-zero exception. */
21            int b = 42 / a;
22            System.out.println("a = " + a);
23            nesttry(a);
24         } catch(ArithmeticException e) {
25             System.out.println("Divide by 0: " + e);
26         }
27     }
28 }

该程序的输出与前面的例子相同。

系列文章:
posted @ 2015-04-30 23:56  Coda  阅读(4076)  评论(0编辑  收藏  举报