开发者

Is there a standard JDK interface which a method like void run() throws Exception?

What I want is a standard JDK class that look like this:

interface ExplodingRunnable {
    void run() throws Exception;
}

Callable is no good, because its call() method is required to return a value, but I need a void.

Runnable is no good, because its run() method doesn't declare throws Exception.

I sort of need a combination开发者_如何学JAVA of the two. Any ideas?

EDIT: I should have mentioned that I tried Callable<Void> but it requires you to define a method:

public Void call() {
    // run your code;
    return null; // ugly!
}

I'm looking for something a bit nicer.

Why do I want this?

I'm implementing a standard why of catching "will never happen" Exceptions (they will never happen, but various APIs define their methods throwing Exceptions) and throwing any Exceptions that might occur by wrapping them in an (unchecked) RuntimeException, so the caller can simply pass a "ExplodingRunnable" in and not have to code loads of perfunctory try/catch blocks that will never be exercised.

FINAL EDIT It looks like what I was looking for doesn't exist. The accepted answer is the closest to "correct", but it looks like there is no solution to answer the question as asked.


Could you just use Callable<Void>?


An interface with only one method, which returns void and throws Exception.

Among all java and javax classes, only one fits that description:

package java.lang;

public interface AutoCloseable
{
    void close() throws Exception;
}

Well... the word "close" has many meanings...


You want to surround a bunch of statements with some extra handling, there is no sin to define your own interface here. You may find that your API requires users to learn 4 new phrases

Util.muckException( new ExplodingRunnable() { public void run() throws Exception
 ^1     ^2                  ^3                             ^4

You can actually cut down two, and user code would look like this

new MuckException(){ public void run() throws Exception
{
    statement_1;
    ...
    statement_n;
}};

public abstract class MuckException
{
    public abstract run() throws Exception;

    public MuckException()
    {
        try{ run(); }
        catch(Exception e){ throw new Error(e); }
    }
}


Just use Callable, ignore the return value and document things as ignoring the returned value and recommend returning null. Just because you can return something does not mean you have to.


I would just use Callable<Void> and learn to love it. ;)

You can have the checked exception not declared with the following.

Runnable runs = new Runnable() {
    public void run() {
        try {
            // do something
        } catch(Exception e) {
            // rethrows anything without the compiler knowing.
            // the method is deprecated but can be used on the current thread.
            Thread.currentThread().stop(e);
        }
    }
});

Future future = executorService.submit(run);
try {
   future.get();
} catch (ExecutionException ee) {
    Throwable e = ee.getCause(); // can be the checked exception above.
}


and not have to code loads of perfunctory try/catch blocks that will never be exercised.

I had the same issue and fixed it a little differently

// Exceptions class
public RuntimeException wrap(Exception e) {
   return e instanceof RuntimeException ? ((RuntimeException)e) : new RuntimeException(e);
}

// user code
try {
  foo.bar();
} catch (Exception e) {
 throw Exceptions.wrap(e);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜