Java Exception convert to String via StringWriter and PrintWriter

Description

Java Exception convert to String via 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);/*ww  w  . j a  v a  2s .  co  m*/
}

  public static String toString(Exception e) {

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



PreviousNext

Related