Skip to main content

How about Throwable convenience method: String getStackTraceString()?

No replies
hnakamur
Offline
Joined: 2003-06-21
Points: 0

How about adding a convenience method: String getStackTraceString() to Throwable?

This already exists in the Bug Parade.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050783

The implementation would be:

public String getStackTraceString() {
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
return sw.toString();
}

I agree with the original poster. There are cases we need a stack trace as a string. I myself had written those lines many times and I thought it would be easier if Throwable had getStackTraceString().

I like getStackTraceString() most among possible method names:

String getMessageWithStrackTrace()
String getStackTraceString()
String getStackTraceAsString
String getFullDescription()

What do you think?