Proposal: Operator to "demote" checked exceptions
Brian Goetz
brian.goetz at oracle.com
Mon Mar 7 20:38:39 UTC 2022
Not going to engage on the “should we give people the opportunity to turn off checked exceptions”, but I will make a few comments on how we might reduce the accidental cost of wrapping and unwrapping exceptions.
> 3. If you want to rethrow the original exceptions, you're forced to catch the wrapper first, and then analyse and downcast the wrapped exceptions.
We’re in the process of adding pattern matching to the language, in stages. Currently patterns are restricted to type patterns, and pattern-aware contexts are restricted to instanceof and switch, but both categories will expand over time.
One context that could become pattern-aware is catch. And deconstruction patterns compose, so we could give exceptions deconstruction patterns to match their wrapping constructors, so that you could say
catch (RuntimeException(SqlException se)) { … }
which reduces the syntactic overhead of unwrapping to basically zero. I’m much more compelled by directions like this, which embrace the tools we have, but reduce their accidental impact. (Obviously there’s work to do to make everything about wrapping this smooth.)
More information about the amber-dev
mailing list