Exposing checked exceptions that a MethodHandle can throw

- liangchenblue at gmail.com
Sun May 21 04:52:44 UTC 2023


Hello,
I am eliciting a discussion on the feasibility of tracking checked
exceptions thrown by a MethodHandle. It is already requested in
https://bugs.openjdk.org/browse/JDK-8268116 as it appears useful in
the development of Foreign Function Interface API.

Currently, explicit MethodHandle usages are hampered by the catch
block as the invoke methods are declared to throw any Throwable. Could
it be possible that we specify the types of possible exceptions at
MethodHandle invocation, so that:
1. Javac accepts code that just call the handle without ugly try-catch block
2. If the exceptions anticipated at invocation site are incompatible
with (i.e. more specific than) those declared by the invoked handle,
it can throw an exception like the existing `WrongMethodTypeException`
eagerly.

Is such a plan feasible? Tracking of exceptions should be easy from
Direct MH and the MethodHandles combinators already, while the
invocation semantics update might be more complex, maybe in the form
of compiler-inserted stubs before the actual invoke calls. I wish such
improvements can make MethodHandle more friendly to direct usages in
code, so users don't need to wrap MH invocations in try-catch blocks
everywhere.

Chen Liang


More information about the core-libs-dev mailing list