try {
throwException();
} catch (Exception e) {
//The exception stack message can be written to
//console or a log file or even we can
//send a mail with the trace.
private static void throwException() { if (true) { throw new RuntimeException("Some user exception");
}
}
public static String getStackTrace(Throwable throwable) {
Writer result = new StringWriter();
PrintWriter printWriter = new PrintWriter(result);
throwable.printStackTrace(printWriter); return result.toString();
}
}
It gives the following output,
****** ERROR ********
java.lang.RuntimeException: Some user exception
at com.bethecoder.tutorials.io.ExceptionTrace.throwException(ExceptionTrace.java:31)
at com.bethecoder.tutorials.io.ExceptionTrace.main(ExceptionTrace.java:15)
****** ERROR ********