异常2

 1     public static void main(String[] args) {
 2         for (int i = 0; i < 4; i++) {
 3             int k;
 4             switch (i) {
 5             case 0:
 6                 try {
 7                     int zero = 0;
 8                     k = 911 / zero;
 9 
10                 } catch (Exception ex) {
11                     System.out.println("算数异常 ,分母不能为零" + ex.getMessage());
12                 }
13                 break;
14             case 1:
15                 try {
16                     int[] b = null;
17                     k = b[0];
18                 } catch (Exception ex) {
19                     System.out.println("空指令异常" + ex.getMessage());
20                 }
21                 break;
22             case 2:
23                 try {
24                     int[] c = new int[2];
25                     k = c[9];
26                 } catch (Exception ex) {
27                     System.out.println("索引超出范围" + ex.getMessage());
28                 }
29                 break;
30             case 3:
31                 try {
32                     char ch = "abc".charAt(99);
33 
34                 } catch (Exception ex) {
35                     System.out.println("索引超出范围" + ex.getMessage());
36                 }
37                 break;
38             }
39 
40         }
41 
42     }

结果:

posted @ 2016-05-28 11:41  唐枫  阅读(156)  评论(0编辑  收藏  举报