File size: 1,318 Bytes
0284885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package Exception_package2;

public class Exception_demo4 {
    public static void main(String[] args) {
        /*
            public String getMessage ()         返回此throwable的详细消息字符串
            public String toString ()           返回此可抛出的简短描述

            public void printStackTrace ()      在底层是利用System.err.println进行输出
                                                把异常的错误信息以红色字体输出在控制台
                                                细节:仅仅是打印信息,不会停止程序运行
         */

        int[] arr = {1, 2, 3, 4, 5, 6};

        try {
            System.out.println(arr[10]);
        } catch (ArrayIndexOutOfBoundsException e) {
            String message = e.getMessage();
            System.out.println(message); //Index 10 out of bounds for length 6

            String str = e.toString();
            System.out.println(str);//java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 6

            e.printStackTrace();
        }

        System.out.println("看看我执行了吗?");

        //正常的输出语句
        System.out.println(123);

        //错误的输出语句(而是用来打印错误信息)
        System.err.println(123);
    }
}