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:
Or read this on Hacker News