Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Spring is the only library I know of with something relatively similar. They have nested exceptions: <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/core/NestedRuntimeException.html" rel="noreferrer">NestedRuntimeException</a> and <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/core/NestedCheckedException.html" rel="noreferrer">NestedCheckedException</a>. These exceptions have useful methods such as <code>getMostSpecificCause()</code> or <code>contains(Class exType)</code>. Their <code>getMessage()</code> method returns the cause's message (it's appended if the wrapping exception already has a message).</p> <p>It's used in Spring's data access exception hierarchy. The idea is that each Database vendor exposes different exceptions in their JDBC drivers. Spring catches those and translates them into more generic <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/dao/DataAccessException.html" rel="noreferrer">DataAccessExceptions</a>. Another benefit of this is that checked exceptions are automatically translated into runtime exceptions.</p> <p>That being said, it's not very complicated code, and I'm sure you could do something similar in your code base. No need to add a Spring dependency just for that.</p> <p>If I were you, I wouldn't try to "unwrap" the exceptions too soon, unless you really can handle them right then and there. I would let them bubble up (wrapped or not), with a global ExceptionHandler that would look at their causal chain, find the first "meaningful" exception, and extract an intelligent error message. When unable to do so, I would just print "technical error" and add the whole stack trace in the detail of the error message or in some kind of log, for bug reports purpose. You'd then fix the bug and/or throw a more meaningful exception instead.</p> <p>Guava's <a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Throwables.html#throwCause%28java.lang.Exception,%20boolean%29" rel="noreferrer">Throwables.getCausalChain()</a> might also be of interest to simplify exception handling:</p> <pre><code>Iterables.filter(Throwables.getCausalChain(e), IOException.class)); </code></pre> <hr> <p>Edit:</p> <p>I thought a little more about your problem, and I think you shouldn't really worry about wrapping your exceptions with a specific "WrapperException" type. You should wrap them using what makes the most sense: either a simple <code>RuntimeException</code> (Guava's <code>Throwables.propagate()</code> might be useful there), a <code>RuntimeException</code> with an additional error message, or a more meaningful exception type when appropriate.</p> <p>Java's causal chain mechanism will let you get to the root cause anyway. You shouldn't need to worry about exception wrapping everywhere in your code. Write a global exception handler to manage it, and use standard exception bubbling everywhere else.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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