JEP draft: Exception handling in switch

Enhancing the switch construct allows handling exceptions from the selector, making switch more useful for pattern matching. Previous updates prevented automatic NullPointerExceptions for null selectors, improving readability. Introducing case throws enables developers to locally handle exceptions directly within the switch, eliminating the need for cumbersome try-catch blocks. Exception cases are separate from normal cases, with precise rethrowing and exhaustive exception handling rules. The new feature supports multiple patterns in a single case label, mimicking multi-catch syntax, and ensures that exceptions from the selector are distinct from other exceptions. This enhancement streamlines static field initialization and avoids workarounds for handling checked exceptions during switch evaluation.

https://openjdk.org/jeps/8323658

To top