异常--Throwsable中的方法

/*
* getMessage()
* 获取异常信息,返回字符串。
* toString()
* 获取异常类名和异常信息,返回字符串。
* printStackTrace()
* 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。
* printStackTrace(PrintStream s)
* 通常用该方法将异常内容保存在日志文件中,以便查阅。
*/

public class ThrowsAbleDemo1 {
    public static void main(String[] args) {
        String[] array = { "1", "2", "3", "4", "5" };
        // 编译时期异常
        try {
            System.out.println(array[5]);// ArrayIndexOutOfBoundsException索引越界异常

        } catch (ArrayIndexOutOfBoundsException e) {
            // 获取异常信息,打印出5
            System.out.println(e.getMessage());
            // 获取异常类名和异常信息,返回字符串。
            System.out.println(e.toString());
            // 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。
            e.printStackTrace();
            // 通常用该方法将异常内容保存在日志文件中,以便查阅。
            // e.printStackTrace(PrintStream s);还没有学习

        }
    }
}

猜你喜欢

转载自blog.csdn.net/My_CODEart/article/details/80449506