<div dir="ltr"><div>Would not that be addressed with the suggested switch-case-throws? See <a href="https://inside.java/2023/12/15/switch-case-effect/" target="_blank">https://inside.java/2023/12/15/switch-case-effect/</a></div><div><br></div><div><span style="font-family:monospace">final String description = switch(thread.getName()) {</span></div><div><span style="font-family:monospace">    case String name -> '"' + name + '"';</span></div><div><span style="font-family:monospace">    case throws </span><span style="font-family:monospace">NullPointerException _ -> "null";<br></span></div><div><span style="font-family:monospace">}</span></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 12 Jul 2024 at 21:24, Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com">archie.cobbs@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I'd like to propose a very minor language improvement and would appreciate any feedback.<br></div><div><br></div><div>This is a true corner case, but I bet most developers have tripped over it a few times. It's easy to work around... but still... <br></div><div><br></div><div>Here's a simple example:</div><div><br></div><div><span style="font-family:monospace">    void describeMyThread(Thread thread) {<br>        final String description;<br>        try {<br>            description = '"' + thread.getName() + "'";<br>        } catch (NullPointerException e) {<br>            description = "null";<br>        }<br>        System.out.println("the thread is " + description);<br>    }</span><br></div><div><br></div><div>This doesn't compile:</div><div><br></div><div><span style="font-family:monospace">    DUTest.java:8: error: variable description might already have been assigned<br>                description = "(null)";<br>                ^</span><br></div><div><br></div><div>The error is a false positive: there is no way an exception can be thrown in the try block <i>after</i> <span style="font-family:monospace">description</span> is assigned, because <span style="font-family:monospace">description</span> being assigned is literally the last thing that occurs in the try block.</div><div><br></div><div>Developers intuitively know that <span style="font-family:monospace">description</span> will be DU at the start of the catch block, so the error feels surprising and makes the compiler seem less smart than it should be.<br></div><div><br></div><div>My proposal is to fix this by adding a "try/catch DU exception" to §16.2.15:</div><div><br></div><div style="margin-left:40px">V is definitely unassigned before a catch block iff all of the following are true:<br>    V is definitely unassigned after the try block <b>or the try block ends with an assignment expression statement V=b and V is definitely unassigned after b</b><br>    V is definitely unassigned before every return statement ...<br></div><div><br></div><div>A prototype compiler implementation is <a href="https://github.com/openjdk/jdk/compare/master...archiecobbs:jdk:TryCatchDUException" target="_blank">here</a>.</div><div><br></div><div>Thoughts?</div><div><br></div><div>-Archie<br></div><div> </div><div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div></div>
</blockquote></div><br clear="all"><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">WBR, Anatoly.</div>