[9] RFR (M): 8050052: Small cleanups in java.lang.invoke code
John Rose
john.r.rose at oracle.com
Fri Jul 18 00:12:23 UTC 2014
On Jul 16, 2014, at 11:20 AM, Peter Levart <peter.levart at gmail.com> wrote:
> An alternative could be:
>
> public Throwable throwIfUnchecked() {
> if (this instanceof RuntimeException) throw (RuntimeException) this;
> if (this instanceof Error) throw (Error) this;
> return this;
> }
>
> Then use it like:
>
> try {
> ...
> } catch (Throwable t) {
> throw new WrapperException(t.throwIfUnchecked());
> }
I like this one. (I wish we could declare This types, so that TYPEOF[t.throwIfUnchecked()] == TYPEOF[t].)
It puts the throw of the "less dangerous" exception type inside the subroutine, making the wrapping and the "more dangerous" (more ad hoc) exception be explicit and in-line.
To complete the picture, add:
public <X extends Throwable> Throwable throwIf(Class<X> exClass) throws X {
if (exClass.isInstance(this)) throw exClass.cast(this);
return this;
}
...to be used as:
try {
...
} catch (Throwable t) {
t.throwIfUnchecked().throwIf(X.class).throwIf(Y.class).throwIf(Z.class);
throw new WrapperException(t);
}
Or some other combination of sequential and/or fluent calls.
— John
More information about the core-libs-dev
mailing list