s-day05-Throwable里面的三个方法
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package com.inmind.throwable05;
|
||||
/*
|
||||
Throwable里面的三个方法
|
||||
String getMessage() 返回此可抛出的简短描述。
|
||||
String toString() 返回此throwable的详细消息字符串。
|
||||
void printStackTrace() 将此throwable的错误信息直接打印在控制台(我们主动打印错误信息)
|
||||
*/
|
||||
public class Demo11 {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
try {
|
||||
System.out.println("try中的方法----开始了【1】");
|
||||
int[] arr = {1, 2, 3, 4, 5};
|
||||
int resutl = getValue(arr,8);
|
||||
System.out.println("try中的方法----结束了【2】");
|
||||
}catch (ArrayIndexOutOfBoundsException e){//ArrayIndexOutOfBoundsException e = new ArrayIndexOutOfBoundsException(msg)
|
||||
System.out.println(e.getMessage());
|
||||
System.out.println(e.toString());
|
||||
e.printStackTrace();//注意:此处是我们程序员主动,将红色异常信息打印到控制台,不会影响程序的执行的,它的打印是【多线程操作引发的】
|
||||
|
||||
System.out.println("捕获了异常----【3】");
|
||||
System.out.println("处理了数组索引越界异常");
|
||||
}
|
||||
System.out.println("程序结束-----【4】");
|
||||
}
|
||||
private static int getValue(int[] arr, int i) {
|
||||
if (i >= arr.length - 1 || i < 0) {
|
||||
String msg = "数组的最大索引为"+(arr.length-1)+",当前索引"+i+"不合法";
|
||||
throw new ArrayIndexOutOfBoundsException(msg);//当前msg就是该异常的详细信息
|
||||
}
|
||||
return arr[i];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user