Java - Write code to convert Exception to String

Requirements

Write code to convert Exception to String

Demo

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

public class Main {
  public static String toString(Exception e) {

    StringWriter s = new StringWriter();
    e.printStackTrace(new PrintWriter(s));
    return s.toString();
  }/* w  ww . j a va 2 s  .c  om*/
}

Related Exercise