<div dir="auto">Currently we have the switch expression as the only expression returning a result.<div dir="auto">Example:</div><div dir="auto">boolean b = switch (1) {</div><div dir="auto"><span style="white-space:pre">        </span>case 0 -> false;</div><div dir="auto"><span style="white-space:pre">      </span>case 1 -> {</div><div dir="auto"><span style="white-space:pre">           </span>yield true;</div><div dir="auto"><span style="white-space:pre">      </span>} </div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">The idea would be to expand this syntax to different expressions for example the try expression:</div><div dir="auto"><br></div><div dir="auto">int i = try {</div><div dir="auto"><span style="white-space:pre">       </span>yield Integer.parseInt("Abc");</div><div dir="auto">} catch (NumberFormatException e) {</div><div dir="auto"><span style="white-space:pre">  </span>yield -1;</div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">Or loops:</div><div dir="auto"><br></div><div dir="auto">String searched = for(String s : args) {</div><div dir="auto"><span style="white-space:pre">     </span>if (s.startsWith("-"))</div><div dir="auto"><span style="white-space:pre">         </span>yield s;</div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">The idea is to make all java control flow expressions able to yield a value. </div><div dir="auto"><br></div><div dir="auto">Among many new patterns possible like the examples above it would for example "clean up" the exception catching initialization:</div><div dir="auto"><br></div><div dir="auto">Foo foo = null;</div><div dir="auto"><br></div><div dir="auto">try {</div><div dir="auto"><span style="white-space:pre">        </span>foo = new Foo();</div><div dir="auto">} catch (SomeException e) {</div><div dir="auto"><span style="white-space:pre">  </span>//do something or nothing </div><div dir="auto">} finally {</div><div dir="auto"><span style="white-space:pre">       </span>//maybe change the value again</div><div dir="auto">} </div><div dir="auto"><br></div><div dir="auto">To</div><div dir="auto"><br></div><div dir="auto">var foo = try {</div><div dir="auto"><span style="white-space:pre">       </span>yield new Foo();</div><div dir="auto">} catch (SomeException e) {</div><div dir="auto"><span style="white-space:pre">  </span>//throw or yield</div><div dir="auto">} finally {</div><div dir="auto"><span style="white-space:pre">  </span>//throw or do nothing </div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">Another benefit especially in this example is the clearer state of the result. In an yielding expression you have to either yield or throw. Also subsequent manipulation in for example the finally block is not possible making the source of the returned result clear to identify.</div><div dir="auto"><br></div><div dir="auto">Great regards </div><div dir="auto">RedIODev </div></div>