转:http://www.blogjava.net/software8/archive/2012/10/24/390144.html
以下是一个异常信息输出的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