将堆栈跟踪转换为字符串的Java程序


将堆栈跟踪转换为字符串的Java程序

import java.io.PrintWriter;

import java.io.StringWriter;

public class PrintTrace {

   public static void main(String[] args) {

      try {

         int d = 0 / 0;

      } catch (ArithmeticException e) {

         StringWriter sw = new StringWriter();

         e.printStackTrace(new PrintWriter(sw));

         String exceptionAsString = sw.toString();

         System.out.println(exceptionAsString);

      }

   }

}