Print exception Information method rollup

Source: Internet
Author: User
Tags stack trace
1. Get the Java exception printstacktrace details

Usually write Java code, want to see the exception information thrown to find out the specific anomaly, we often use exception.tostring () or exception.getmessage () to get the exception information, and then print it to the console, But this information can only tell us the exception itself, and it's not ideal for us to find the anomaly, so we'll use the Exception.printstacktrace () method so we can output very detailed exception information at the console. It can even be traced to the first few lines of the class where the exception occurred, which is very useful to us. But sometimes we just want to get these stacktrace data and show it in other ways (not consoles, like web pages or GUIs), and that's the article. Recall that the original log tool log4, WebLogic server, There are webshpere servers and so on seem to be able to get this information, so first directly in the exception class to find there is no direct way to return this information, see Getstacktrace () method returns a collection of Stacktraceelement objects (why didn't you notice it?) ), flip the JDK, and look at the methods provided by the Stacktraceelement class (

String GetFileName ()
Returns the name of the source file containing the execution point represented by this stack trace element.
Int Getlinenumber ()
Returns the line number of the "source line containing" execution point represented by this stack trace element.
String Getmethodname ()
Returns the name of the method containing the execution point represented by this stack trace element.
Int hashcode ()
Returns a hash code value for this stack trace element.
Boolean Isnativemethod ()
Returns true if the method containing the execution point represented by this stack trace element are a native method.
String toString ()
Yes, that's him, write a piece of code to test the first:

public static void Main (string[] args) {
try{
Byte[] A=args[0].getbytes ();

}catch (Exception ex) {

Ex.printstacktrace ();
Stacktraceelement [] Messages=ex.getstacktrace ();
int length=messages.length;
for (int i=0;i<length;i++) {
System.out.println ("ClassName:" +messages[i].getclassname ());
System.out.println ("GetFileName:" +messages[i].getfilename ());
System.out.println ("Getlinenumber:" +messages[i].getlinenumber ());
System.out.println ("Getmethodname:" +messages[i].getmethodname ());
System.out.println ("toString:" +messages[i].tostring ());
}
}
}

2.

Import Java.io.PrintWriter;
Import Java.io.StringWriter;

public class Printwritertest {
public static void Main (string[] args) {
try{
Exception ex = new Exception ("AAA");
Throw ex;
}catch (Exception e) {
StringWriter SW = new StringWriter ();
E.printstacktrace (new PrintWriter (SW));
System.out.println ("*****=" +sw.getbuffer (). toString ());
}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.