Java Exception stack information turn string

Source: Internet
Author: User

Usually use E.getmessage () or e.printstacktrace ();

The first kind of the exception to throw the new RuntimeException ("Exception"), can only get an exception of 2 words.

The second type of e.printstacktrace (); Just prints and does not return any data.

While the server is generally clustered or other methods of deployment, view the log is too cumbersome, you can stack information into a string and other types, and then save to the database, placed in the hidden field of the page for easy viewing.

There are 3 ways to choose from:

Method One:

package name.xu;public class callstack {    public static  VOID&NBSP;PRINTCALLSTATCK ()  {        throwable ex =  new throwable ();        stacktraceelement[]  Stackelements = ex.getstacktrace ();                 if  (stackelements != null)  {                          for  (int i = 0; i < stackelements.length; i++)  {                  System.out.print (Stackelements[i].getclassname () + "/t");                 system.out. Print (Stackelements[i].getfilename () + "/t");                 system.out.print (Stackelements[i].getlinenumber () + "/t");                 system.out.println (StackElements[i]. Getmethodname ());                 system.out.println ("-----------------------------------");             }        }    }     }


Method Two:

Exception e = new Exception ("This is a log"); E.printstacktrace ();

Method Three:

String fullstacktrace = Org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace (e)

Method Four:

Thread.CurrentThread (). Getstacktrace ()

Personal recommendation, method One, reason: easy to use, fast


Java Exception stack information turn string

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.