Why does `ScopedValue.where(SCOPE_VALUE, val).call()` throw Exception?
Andrew Haley
aph-open at littlepinkcloud.com
Thu Mar 23 10:28:36 UTC 2023
On 3/23/23 02:52, Josiah Noel wrote:
> Is there a specific reason that call() throws the checked exception, but
> not run()?
Yes.
The signature of Callable.call() is: V call() throws Exception
The signature of Runnable.run() is: void run()
Something has to catch the exception.
A previous version of the ScopedValue API had the following method. It was intended for your
situation.
/**
* Run a value-returning operation with this set of ScopeLocals bound to values,
* in the same way as {@code call()}.<p>
* If the operation throws an exception, pass it as a single argument to the {@link Function}
* {@code handler}. {@code handler} must return a value compatible with the type returned by {@code op}.
* </p>
* @param op the operation to run
* @param <R> the type of the result of the function
* @param handler the handler to be applied if {code op} threw an exception
* @return the result.
*/
public final <R> R callOrElse(Callable<R> op,
Function<? super Exception, ? extends R> handler) {
try {
return call(op);
} catch (Exception e) {
return handler.apply(e);
}
}
--
Andrew Haley (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
More information about the loom-dev
mailing list