MethodHandles.CatchException(MethodHandle, Class, MethodHandle) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Makes a method handle which adapts a target method handle, by running it inside an exception handler.
[Android.Runtime.Register("catchException", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/Class;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=26)]
public static Java.Lang.Invoke.MethodHandle? CatchException (Java.Lang.Invoke.MethodHandle? target, Java.Lang.Class? exType, Java.Lang.Invoke.MethodHandle? handler);
[<Android.Runtime.Register("catchException", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/Class;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=26)>]
static member CatchException : Java.Lang.Invoke.MethodHandle * Java.Lang.Class * Java.Lang.Invoke.MethodHandle -> Java.Lang.Invoke.MethodHandle
Parameters
- target
- MethodHandle
method handle to call
- exType
- Class
the type of exception which the handler will catch
- handler
- MethodHandle
method handle to call if a matching exception is thrown
Returns
method handle which incorporates the specified try/catch logic
- Attributes
Remarks
Makes a method handle which adapts a target method handle, by running it inside an exception handler. If the target returns normally, the adapter returns that value. If an exception matching the specified type is thrown, the fallback handle is called instead on the exception, plus the original arguments.
The target and handler must have the same corresponding argument and return types, except that handler may omit trailing arguments (similarly to the predicate in #guardWithTest guardWithTest
). Also, the handler must have an extra leading parameter of exType
or a supertype.
Here is pseudocode for the resulting adapter. In the code, T
represents the return type of the target
and handler
, and correspondingly that of the resulting adapter; A
/a
, the types and values of arguments to the resulting handle consumed by handler
; and B
/b
, those of arguments to the resulting handle discarded by handler
. <blockquote>
{@code
T target(A..., B...);
T handler(ExType, A...);
T adapter(A... a, B... b) {
try {
return target(a..., b...);
} catch (ExType ex) {
return handler(ex, a...);
}
}
}
</blockquote> Note that the saved arguments (a...
in the pseudocode) cannot be modified by execution of the target, and so are passed unchanged from the caller to the handler, if the handler is invoked.
The target and handler must return the same type, even if the handler always throws. (This might happen, for instance, because the handler is simulating a finally
clause). To create such a throwing handler, compose the handler creation logic with #throwException throwException
, in order to create a method handle of the correct return type.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.