1、Throwable类
Throwable类有几个非常重要的方法:
String getMessage():获得发生异常的详细信息。
void printStackTrace():打印异常堆栈跟踪信息。
String toString():获得异常对象的描述。
代码示例
public class Catch {
public static void main(String[] args) {
int a =0;
int result = divide(5,a);
System.out.printf("divide(%d,%d) = %d",5,a,result);
}
private static int divide(int number, int divisor) {
// 可以判断除数divisor非零,防止运行时异常
// if(divisor !=0){
// return number / divisor;
// }
//
try {
return number / divisor;
} catch (Throwable throwable) {
System.out.println("getMessage():"+throwable.getMessage());
System.out.println("toString():"+throwable.toString());
System.out.println("printStackTrace()输出信息如下:");
//打印堆栈信息
throwable.printStackTrace();
}
return 0;
}
}
2、Error和Exception
3、受检查异常和运行时异常