multi-catch vs. multiple type patterns in a single case label
Ethan McCue
ethan at mccue.dev
Fri Apr 8 16:19:00 UTC 2022
The answer is that a switch does not yet support an "or" pattern. It could
in the future, but the exact shape of switch patterns is in flux.
On Fri, Apr 8, 2022, 10:05 AM Tobias Gierke <tobias.gierke at code-sourcery.de>
wrote:
> Hello,
>
> Having read through JEP-420, I wonder why
>
> try {
> .... do stuff ....
> } catch( IOException | IllegalStateException e) {
> System.out.println("Something went wrong: "+e);
> }
>
> is valid but
>
> Exception x = ....
> switch(x) {
> case IOException | TimeoutException e ->
> System.out.println("Something went wrong: "+e);
> }
>
> is not. I get that something like
>
> Exception x = ....
> switch(x) {
> case IOException a, IllegalStateException b -> .....
> }
>
> would leave one of the variables undefined but why can't I bind multiple
> type patterns to the same variable, have the compiler infer a common
> super-type and use that to check the switch branch ?
>
> Regards,
> Tobias
>
>
More information about the discuss
mailing list