课堂测试第八周

一.java异常处理机制

1.用try包围可能异常的代码块,当程序出现异常时系统会抛出异常,异常处理代码会捕获异常。

2.catch代码块用来处理异常,当异常发生时,从程序会从try代码块跳到catch代码块。

3.不管有无异常发生,finally代码块中的语句都会执行。

4.如果没有合适的异常处理代码,jvm会结束整个应用程序。

二.try、catch抛出和处理机制。

 1 public class CatchWho2 { 
 2     public static void main(String[] args) { 
 3         try {
 4                 try { 
 5                     throw new ArrayIndexOutOfBoundsException(); 
 6                 } 
 7                 catch(ArithmeticException e) { 
 8                     System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
 9                 }
10             throw new ArithmeticException(); 
11         } 
12         catch(ArithmeticException e) { 
13             System.out.println("发生ArithmeticException"); 
14         } 
15         catch(ArrayIndexOutOfBoundsException e) { 
16             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
17         } 
18     } 
19 }

 出现这样的输出结果是因为内层try抛出的异常类型和第一个catch捕获类型不同,运行到下一个catch语句。

3.

 1 public class EmbededFinally {
 2 
 3     
 4     public static void main(String args[]) {
 5         
 6         int result;
 7         
 8         try {
 9             
10             System.out.println("in Level 1");
11 
12            
13              try {
14                 
15                 System.out.println("in Level 2");
16   // result=100/0;  //Level 2
17                
18                  try {
19                    
20                      System.out.println("in Level 3");
21                       
22                      result=100/0;  //Level 3
23                 
24                 } 
25                 
26                 catch (Exception e) {
27                     
28                     System.out.println("Level 3:" + e.getClass().toString());
29                 
30                 }
31                 
32                 
33                 finally {
34                     
35                     System.out.println("In Level 3 finally");
36                 
37                 }
38                 
39                
40                 // result=100/0;  //Level 2
41 
42             
43                 }
44             
45             catch (Exception e) {
46                
47                  System.out.println("Level 2:" + e.getClass().toString());
48            
49              }
50              finally {
51                 
52                 System.out.println("In Level 2 finally");
53            
54              }
55              
56             // result = 100 / 0;  //level 1
57         
58         } 
59         
60         catch (Exception e) {
61             
62             System.out.println("Level 1:" + e.getClass().toString());
63         
64         }
65         
66         finally {
67            
68              System.out.println("In Level 1 finally");
69         
70         }
71     
72     }
73 
74 }

输出结果:

 在此结果中三个finally语句都执行了,但是由于catch语句的对象是Exception类型,执行完后后面的catch语句不在执行,所以只执行level3语句。

4.

 1 public class SystemExitAndFinally {
 2 
 3     
 4     public static void main(String[] args)
 5     {
 6         
 7         try{
 8 
 9             
10             System.out.println("in main");
11             
12             throw new Exception("Exception is thrown in main");
13 
14                     //System.exit(0);
15 
16         
17         }
18         
19         catch(Exception e)
20 
21             {
22             
23             System.out.println(e.getMessage());
24             
25             System.exit(0);
26         
27         }
28         
29         finally
30         
31         {
32             
33             System.out.println("in finally");
34         
35         }
36     
37     }
38 
39 
40 }

 在此与语句中由于catch语句中有exit(0);使程序提前终结所以finally语句不执行。

猜你喜欢

转载自www.cnblogs.com/w669399221/p/11774122.html