Method One:
Private StaticString getstackmsg (Exception e) {stringbuffer sb=NewStringBuffer (); Stacktraceelement[] Stackarray=E.getstacktrace (); for(inti = 0; i < stackarray.length; i++) {stacktraceelement element=Stackarray[i]; Sb.append (element.tostring ()+ "\ n"); } returnsb.tostring (); } Private StaticString getstackmsg (Throwable e) {stringbuffer sb=NewStringBuffer (); Stacktraceelement[] Stackarray=E.getstacktrace (); for(inti = 0; i < stackarray.length; i++) {stacktraceelement element=Stackarray[i]; Sb.append (element.tostring ()+ "\ n"); } returnsb.tostring (); }
Method Two:
PackageCom.test1;ImportJava.io.PrintWriter;ImportJava.io.StringWriter; Public classT010 {/** * @paramargs*/ Public Static voidMain (string[] args) {Try{string[] arr= {"111", "222"}; arr[2] = "FFF"; } Catch(Exception e) {String info=geterrorinfofromexception (e); SYSTEM.OUT.PRINTLN (info); } } Public StaticString geterrorinfofromexception (Exception e) {Try{StringWriter SW=NewStringWriter (); PrintWriter PW=NewPrintWriter (SW); E.printstacktrace (PW); return"\ r \ n" + sw.tostring () + "\ r \ n"; } Catch(Exception E2) {return"Bad Geterrorinfofromexception"; } }}
Convert exception (getstacktrace) to string