Time to put a stop to Thread.stop?
Doug Lea
dl at cs.oswego.edu
Fri May 24 18:58:28 UTC 2013
On 05/24/13 14:14, Martin Buchholz wrote:
> Ordinary java code should be able to simply catch and rethrow a Throwable if
> type analysis can "prove" that the exception is not an Exception.
>
> As a data point, Doug uses this:
>
> (Doug, it's not obvious to me why you handle Error and RuntimeException specially)
>
This was initially done when the unchecked-throw was via
Unsafe.throwException, needed only in those cases.
But then we switched to using sneaky-throw, relying on
the same javac type-check limitation everyone else relies on
these days. Which makes it more portable across JVMs, but could in
theory stop working someday. But until then,
those checks aren't needed and can/should go away.
-Doug
> /**
> * A version of "sneaky throw" to relay exceptions
> */
> static void rethrow(final Throwable ex) {
> if (ex != null) {
> if (ex instanceof Error)
> throw (Error)ex;
> if (ex instanceof RuntimeException)
> throw (RuntimeException)ex;
> ForkJoinTask.<RuntimeException>uncheckedThrow(ex);
> }
> }
>
> /**
> * The sneaky part of sneaky throw, relying on generics
> * limitations to evade compiler complaints about rethrowing
> * unchecked exceptions
> */
> @SuppressWarnings("unchecked") static <T extends Throwable>
> void uncheckedThrow(Throwable t) throws T {
> if (t != null)
> throw (T)t; // rely on vacuous cast
> }
>
>
More information about the core-libs-dev
mailing list