31.1 Exception 的method :getMessage()、 printStackTrace()

package day31_exception;

import java.lang.Exception;

/*
 * Throwable的常用方法:
        String getMessage()  :原因
        String toString()  :类型和原因
        void printStackTrace()  :类型原因和位置
 */
public class Exception的method {
    public static void main(String[] args) {
//        int[] arr = {1,2,3};
//        System.out.println(arr[3]);

        try {
            int[] arr = {1,2,3};
            System.out.println(arr[4]);
        } catch (Exception e) {
            e.printStackTrace(); //printStackTrace输出和系统输出的报错一致,优点在于这个报错是try报出的不会使程序退出
            System.out.println(e.toString());
//            System.out.println(e.getMessage());
        }

        System.out.println("hello");
    }
}

输出

原文地址:https://www.cnblogs.com/longesang/p/11290542.html