转自:http://hi.baidu.com/johnnycode/item/3b945e3fb07395fc97f88d43
看下面例子:
public class Test {
public static void main(String[] args) {
try {
System.out.println(1 / 0);
} catch (Exception e) {
System.out.println(e.toString());
System.out.println("--------------------");
System.out.println(e.getMessage());
System.out.println("--------------------");
e.printStackTrace();
}
}
}
输出:
java.lang.ArithmeticException: / by zero
--------------------
/ by zero
--------------------
java.lang.ArithmeticException: / by zero
at com.sun.sequence.Test.main(Test.java:7)
总结:
e.toString(): 获得异常种类和错误信息
e.getMessage():获得错误信息
e.printStackTrace():在控制台打印出异常种类,错误信息和出错位置等
打印 java异常
本文转载:CSDN博客