Skip to main content
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How can I convert a stack trace to a string?

What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace?

Answer*

Cancel
8
  • 9
    This does trim the stack trace, the same way printStackTrace(). All exceptions in the stack are visible but for each exception the stack may be trimmed. Anyone requiring the entire trace should consider this.
    Laila Agaev
    –  Laila Agaev
    2013-11-29 20:20:01 +00:00
    Commented Nov 29, 2013 at 20:20
  • 9
    This, as it turns out, is pretty much exactly what apache's ExceptionUtils.getStackTrace() method does. Almost to the letter actually.
    ticktock
    –  ticktock
    2014-03-04 22:30:51 +00:00
    Commented Mar 4, 2014 at 22:30
  • 7
    @BrianAgnew, shouldn't you close the StringWriter and PrintWriter ?
    Muhammad Gelbana
    –  Muhammad Gelbana
    2015-08-02 21:07:33 +00:00
    Commented Aug 2, 2015 at 21:07
  • 6
    @MuhammadGelbana - yes. The above is shown for expediency. I suspect if you don't it wouldn't cause a problem, but I would advocate it on grounds of good practise
    Brian Agnew
    –  Brian Agnew
    2015-08-03 08:42:54 +00:00
    Commented Aug 3, 2015 at 8:42
  • 17
    @BrianAgnew, thanks for the reply. It got me curious and here is what I found: stackoverflow.com/questions/14542535/…
    Muhammad Gelbana
    –  Muhammad Gelbana
    2015-08-03 09:31:59 +00:00
    Commented Aug 3, 2015 at 9:31

Morty Proxy This is a proxified and sanitized view of the page, visit original site.