Get exception information e. printstacktrace ()
The operation logs need to be recorded during the recent project, but the exception information is found to use E. getmessage () cannot meet the requirements at all, and E. getmessage () Sometimes the obtained information does not know the specific error information, so we need to obtain e. content of printstacktrace ()
Get Exception error information
Package COM. gavin. exception. demo; import Java. io. printwriter; import Java. io. stringwriter; import Java. io. writer; public class test {public static void main (string [] ARGs) {try {method ();} catch (exception E) {// todo auto-generated Catch Block writer = new stringwriter (); E. printstacktrace (New printwriter (writer); system. out. println ("print error:" + writer. tostring (); E. printstacktrace () ;}} public static void method () throws exception {Throw new exception ("error ");}}
Output result:
Print error: Java. Lang. Exception: Error
At com. Demo. Test. Method (test. Java: 23)
At com. Demo. Test. Main (test. Java: 11)
Java. Lang. Exception: Error
At com. Demo. Test. Method (test. Java: 23)
At com. Demo. Test. Main (test. Java: 11)