Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That needs three slightly different answers:</p> <ol> <li><p><strong>Yes</strong>, you can wrap an exception in another exception of the <em>same type</em>.</p></li> <li><p><strong>No</strong>, you can't wrap an exception in <em>itself</em> (i.e. in the exact same instance).</p></li> <li><p>Unfortunately <strong>yes</strong>, you can create a loop (A causing B causing A causing B ...).</p></li> </ol> <p>The first one is pretty clear: you can wrap an <code>IllegalStateException</code> wrapping an <code>IllegalArgumentException</code> caused by another <code>IllegalStateException</code>.</p> <p>The second one is prevented by the code in <code>initClause()</code> (which is called by the constructor or can be called directly if it was never called before), that prevents self-causation (indeed <code>cause == this</code> is used as a flag that no cause was set, to distinguish it from <code>cause == null</code> which means the cause was explicitly set to <code>null</code>).</p> <p>The third bit is bad, but shouldn't happen too often in practice, as you'd have to do some extra work to get it:</p> <pre><code>Exception e1 = new Exception(); Exception e2 = new Exception(e1); e1.initCause(e2); </code></pre> <p>Luckily <code>printStackTrace()</code> actually handles that case:</p> <pre><code>java.lang.Exception: java.lang.Exception at ScratchMain.main(ScratchMain.java:6) Caused by: java.lang.Exception at ScratchMain.main(ScratchMain.java:5) [CIRCULAR REFERENCE:java.lang.Exception: java.lang.Exception] </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. 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