Time to put a stop to Thread.stop?

Martin Buchholz martinrb at google.com
Wed May 29 01:23:56 UTC 2013


[+compiler-dev]


On Wed, May 15, 2013 at 1:05 AM, David Holmes <david.holmes at oracle.com>wrote:

> On 15/05/2013 3:16 PM, Martin Buchholz wrote:
>
>>
>>
>> General purpose library code sometimes would like to rethrow an
>> exception that was previously caught.
>> How should it do that?
>>
>
> Umm catch it and throw it. If it is a checked-exception that you want to
> propogate then you should have declared it on your method, else you are
> going to wrap it in a runtime exception or error. There is no need for such
> sleaze.
>
>
Taking a closer look at one use of Thread.stop, I see that we use it to
throw a Throwable out of Callable.call.  Which I think we should be able to
do.  But I can't.

 cat CallableThrow.java && javac CallableThrow.java

public class CallableThrow {
    public static void main(String[] args) throws Throwable {
        final Throwable t = new Throwable();
        new java.util.concurrent.Callable<Void>() {
            public Void call() throws Exception { throw t; }};
    }
}
CallableThrow.java:5: error: unreported exception Throwable; must be caught
or declared to be thrown
            public Void call() throws Exception { throw t; }};
                                                  ^
1 error


If I change Exception to Throwable I get:

public class CallableThrow {
    public static void main(String[] args) throws Throwable {
        final Throwable t = new Throwable();
        new java.util.concurrent.Callable<Void>() {
            public Void call() throws Throwable { throw t; }};
    }
}
CallableThrow.java:5: error: call() in <anonymous CallableThrow$1> cannot
implement call() in Callable
            public Void call() throws Throwable { throw t; }};
                        ^
  overridden method does not throw Throwable
  where V is a type-variable:
    V extends Object declared in interface Callable
1 error



More information about the core-libs-dev mailing list