Get the latest tech news

JEP draft: Exception handling in switch


(Preview) Summary Enhance the switch construct so that its block can handle exceptions thrown by the selector (the e in switch (e) ...). Alongside a previous enhancement to handle null selectors, this makes switch even more useful for pattern matching.

Other exceptions that can be thrown from a switch block may originate from the evaluation of the guard or from the right-hand side of a case label. While it might seem like an obvious move to propagate multi-catch into exception cases, this may foreclose on other, potentially more valuable possibilities, so we will consider this for the future. The verbosity of the working example shown in the Motivation could be mitigated by extracting the complexity of try- catch to a helper method and mapping the potential result to a suitable type, such as Optional:

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Switch

Switch

Photo of exception

exception

Photo of JEP

JEP

Related news:

News photo

Nintendo won't be at Gamescom 2024, and neither will the Switch 2

News photo

Discord has banned two Switch emulator devs and shuttered their servers in the wake of Yuzu's defeat

News photo

Sarcophagus Is a Dead Man’s Switch for Your Crypto Wallet