Note that there are some explanatory texts on larger screens.

plurals
  1. POException from resource close in finally hides another -- who cares?
    primarykey
    data
    text
    <p>Firstly, sorry for asking a question on what must be a well-worn topic. I've come across loads of questions with answers suggesting to swallow (catch and ignore/log) potential exception from methods that close resources in finally blocks. It seems to be a generally accepted pattern. But I haven't yet seen anybody explain <em>why</em>. Here's just one example: <a href="https://stackoverflow.com/questions/1335812/try-catch-finally-and-then-again-a-try-catch">Try-catch-finally and then again a try catch</a>.</p> <p>I understand that any exception thrown from a finally block will "mask" any exception thrown in the corresponding try block, but I don't see why this is a bad thing. For example:</p> <pre><code>Resource r = new Resource(); try { r.use(); other(); } finally { r.close(); } </code></pre> <p>My current understanding is:</p> <ol> <li>If only <code>close</code> throws an exception, we definitely don't want to swallow it.</li> <li>If both <code>use</code> and <code>close</code> throw an exception, it's probably for the same underlying reason, and it doesn't matter which exception gets propagated up (they both would contain equally useful information?).</li> <li>If both <code>other</code> and <code>close</code> throw an exception, there are two unrelated problems. Arguably the one that happened first should propagate, but there isn't any reason to suggest that the first exception <em>caused</em> the second (is there?), so either will do.</li> </ol> <p>What am I wrong about?</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.
 

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