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();
  }//from w w  w .  j a v a  2  s  .c  o m
}

Related Exercise