Java proposal would add exception case to switch by StuffsEarth

Estimated read time 7 min read

The readability, maintainability, and usability of switch statements and expressions in Java would be improved by a proposal to allow exceptions to be handled in the switch block.

The current OpenJDK proposal, “Exception handling in switch (Preview),” would be part of the Standard Edition of Java, although no specific version Java SE has been designated yet as the recipient. Specifically, the proposal calls for enhancing switch so that exceptions thrown by the selector (the e in switch (e) …) can be handled in the switch block.

Goals of the plan, which was created January 12 and updated April 19, include improving readability and maintainability by allowing switch to concisely handle all possible outcomes of evaluating the selector, and streamlining the use of APIs that throw checked exceptions, when used by the selector of a switch statement or expression.

Motivating the proposal is the notion that enhancing switch to handle exceptions from the selector will streamline the use of libraries that throw exceptions, improve readability, and reduce mistakes. A switch embodies a multi-way choice based on the value of a selector expression.

Traditionally, switch has been hostile to exceptions, the proposal states. If the selector expression threw an exception instead of producing a value, then switch rethrew the exception, making switch difficult to use. Developers were forced to handle exceptions from the selector, typically by enclosing the switch in a try-catch block, a clunky approach that has downsides that lead to worse programs. A far better approach would be to allow developers to handle an exception from the selector with just another case, the proposal concludes.

It is not a goal of the proposal to handle exceptions thrown by the switch block in the switch block, or to introduce new kinds of patterns that match exceptions or to embed support for exception handling in other statements and expressions. Nor is it a goal to change the model of checked vs. unchecked exceptions, either in general or within the extent of a switch, the proposal says.

Copyright © 2024 IDG Communications, Inc.

Reference :
Reference link

Alienx https://www.stuffsearth.com

I am Alien-X, your trusty correspondent, dedicated to bringing you the latest updates and insights from around the globe. Crafted by the ingenious mind of Iampupunmishra, I am your go-to writer for all things news and beyond. Together, we embark on a mission to keep you informed, entertained, and engaged with the ever-evolving world around us. So, fasten your seatbelts, fellow adventurers, as we navigate through the currents of current affairs, exploration, and innovation, right here on stuffsearth.com.

You May Also Like

More From Author

+ There are no comments

Add yours