Java堆栈跟踪到字符串


Java堆栈跟踪到字符串

import java.io.PrintWriter;

import java.io.StringWriter;



public class StackTraceToString {

   public static void main(String args[]) {

      try {

         int a[] = new int[2];

         System.out.println("Access element three :" + a[3]);

      } catch (ArrayIndexOutOfBoundsException e) {

         StringWriter writer = new StringWriter();

         e.printStackTrace(new PrintWriter(writer));



         String myString = writer.toString();

         System.out.println("Stack trace message ::"+myString);

      }

   }

}