Skip to main content

How about Throwable convenience method: String getStackTraceString()?

No replies
Joined: 2003-06-21

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

This already exists in the Bug Parade.

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?