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():在控制台打印出异常种类,错误信息和出错位置等
没有评论:
发表评论