以下是一個異常信息輸出的demo,學會在異常處理中輸出所需要的信息,是e.printStackTrace()、e.getMessage、還是e.toString
public class testException
{
public static void main(String[] args)
{
try
{
byte[] a = args[0].getBytes();
}
catch (Exception e)
{
System.out.println("logy提示異常stackTrace:");
e.printStackTrace();
System.out.println("logy提示異常message:");
System.out.println("logy提示異常:"+e.getMessage());
System.out.println("logy提示異常e.tostring");
System.out.println(e.toString());
System.out.println("logy提示異常e.getMessage.toString");
System.out.println(e.getMessage().toString());
System.out.println("詳情");
StackTraceElement[] messages=e.getStackTrace();
for(StackTraceElement i : messages)
{
System.out.println("類名:"+i.getClassName());
System.out.println("文件名:"+i.getFileName());
System.out.println("行號:"+i.getLineNumber());
System.out.println("方法名:"+i.getMethodName());
System.out.println("信息:"+i.toString());
}
}
}
}
運行結果如下
logy提示異常stackTrace:
java.lang.ArrayIndexOutOfBoundsException: 0
at testException.main(testException.java:8)
logy提示異常message:
logy提示異常:0
logy提示異常e.tostring
java.lang.ArrayIndexOutOfBoundsException: 0
logy提示異常e.getMessage.toString
0
詳情
類名:testException
文件名:testException.java
行號:8
方法名:main
信息:testException.main(testException.java:8)
e.getmessage < e.tostring < e.getStackTrace +e.tostring = e.printStackTrace