Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All this amounts to exploiting the loophole that an unchecked cast to a generic type is not a compiler error. Your code is explicitly made type-unsafe if it contains such an expression. And since the checking of checked exceptions is strictly a compile-time procedure, nothing will break in the runtime.</p> <p>The answer from the authors of Generics would most probably be along the lines of "If you are using unchecked casts, it's your problem".</p> <p>I see something quite positive in your discovery&mdash;a break out of the stronghold of checked exceptions. Unfortunately, this can't turn existing checked-exception-poisoned APIs into something more pleasant to use.</p> <h2>How this can help</h2> <p>At a typical layered application project of mine there will be a lot of boilerplate like this:</p> <pre><code>try { ... business logic stuff ... } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } </code></pre> <p>Why do I do it? Simple: there are no business-value exceptions to catch; any exception is a symptom of a runtime error. The exception must be propagated up the call stack towards the global exception barrier. With Lukas' fine contribution I can now write</p> <pre><code>try { ... business logic stuff ... } catch (Exception e) { throwUnchecked(e); } </code></pre> <p>This may not seem like much, but the benefits accumulate after repeating it 100 times throughout the project.</p> <h2>Disclaimer</h2> <p>In my projects there is high discipline regarding exceptions so this is a nice fit for them. <strong>This kind of trickery is not something to adopt as a general coding principle</strong>. Wrapping the exception is still the only safe option in many cases.</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.
    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.
 

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