注意:Throwable 类是 Java 语言中所有错误或异常的超类(父类)。只有当对象是 Throwable 类(或其子类)的实例的时候,才能通过 Java 的 throw 语句抛出。当然,只有是 Throwable 类(或其子类)才可以是 catch 子句中的参数类型。
为了保证程序的正常执行,代码必须对可能出现的异常进行处理。
示例:
package com.github.demo1;
/**
* 发生异常的时候,如果没有进行处理,会一层一层的向上抛出,
最终交给JVM处理,JVM会终止程序,输出对应的信息。
*
* @author maxiaoke.com
* @version 1.0
* @return
*/
public class TestException {
public static void main(String[] args) {
System.out.println("main方法");
show();
}
public static void show() {
System.out.println("show方法");
int[] arr = {1, 2};
System.out.println(arr[2]);
System.out.println("show方法调用结束");
}
}