Java Exception print stack trace to StringWriter and PrintWriter

Description

Java Exception print stack trace to StringWriter and PrintWriter


import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
  public static void main(String[] argv) {
    String s = toString(new Exception("demo2s.com"));
    System.out.println(s);//from   www  . ja  v a2s  . c om
}

  public static String toString(Exception e) {

    StringWriter s = new StringWriter();
    e.printStackTrace(new PrintWriter(s));
    return s.toString();
  }
}



PreviousNext

Related