Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to log as much information as possible for a Java Exception?
    primarykey
    data
    text
    <p>There's a common problem I've come across a few times when logging Exceptions. There seem to be various different types to deal with. E.g. some wrap other Exceptions, some don't have a message at all - just a type.</p> <p>Most code I've seen logs Exceptions using either <code>getMessage()</code> or <code>toString(</code>) but these don't always capture all the information needed to pinpoint the problem - other methods such as <code>getCause()</code> and <code>getStackTrace()</code> sometimes provide additional info.</p> <p>As an example, the Exception I'm looking at right now in my Eclipse Inspect window is an <code>InvocationTargetException</code>. The Exception itself has no cause, no message, no stacktrace ... but the target from getCause() is <code>InvalidUseOfMatchersException</code> with these details populated.</p> <p>So my question is: Given an Exception of <em>any</em> type as an input please provide a single method that will output a nicely formatted String containing <em>all</em> relevant information about the Exception (e.g. possibly recursively calling <code>getCause()</code> amongst other things?) Before posting this question I was nearly going to have a stab at this myself but don't really want to reinvent the wheel - surely such a thing must have been done many times before...?</p> <p><em>Please don't point me at any particular logging or utility framework to do this. I'm looking for a fragment of code rather than a library since I don't have the right to add external dependencies on the project I'm working on and it's actually being logged to part of a webpage rather than a file. If it's a case of copying the code fragment out of such a framework (and attributing it) that's fine :-)</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload