Java异常捕获

 1 public class Main {
 2 
 3     /**
 4      * 在写异常处理的时候,一定要把异常范围小的放在前面,范围大的放在后面,
 5      * Exception这个异常的根类一定要刚在最后一个catch里面,
 6      * 如果放在前面或者中间,任何异常都会和Exception匹配的,就会报已捕获到异常的错误。
 7      * @param args
 8      */
 9     public static void main(String[] args) {
10         int a[] = new int[] {1,2,3,4,5};
11         try {
12             System.out.println(a[6]);
13         
14         } catch (ArrayIndexOutOfBoundsException ae) {
15             System.out.println("Catching ArrayIndexOutOfBounds"
16                     + " Exception ..."); 
17         } catch (Exception e) {
18              System.out.println("Catching Exception ...");
19         }
20                 
21     }
22 
23 }

输出:Catching ArrayIndexOutOfBounds Exception ...

posted @ 2014-08-13 21:28  soul390  阅读(150)  评论(0)    收藏  举报