Is there a way to make Runnable's run() throw an exception?

前端 未结 9 1153
忘了有多久
忘了有多久 2020-12-07 14:43

A method I am calling in run() in a class that implements Runnable) is designed to be throwing an exception.

But the Java compiler won\'t let me do that and suggests

相关标签:
9条回答
  • 2020-12-07 15:29
    @FunctionalInterface
    public interface CheckedRunnable<E extends Exception> extends Runnable {
    
        @Override
        default void run() throws RuntimeException {
            try {
                runThrows();
            }
            catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    
        void runThrows() throws E;
    
    }
    
    0 讨论(0)
  • 2020-12-07 15:30

    Your requirement doesn't make any sense. If you want to notify the called of the thread about an exception that happened, you could do that through a call back mechanism. This can be through a Handler or a broadcast or whatever else you can think of.

    0 讨论(0)
  • 2020-12-07 15:32

    The easiest way is to define your own exception object which extend the RuntimeException class instead of the Exception class.

    0 讨论(0)
  • 2020-12-07 15:35

    Some people try to convince you that you have to play by the rules. Listen, but whether you obey, you should decide yourself depending on your situation. The reality is "you SHOULD play by the rules" (not "you MUST play by the rules"). Just be aware that if you do not play by the rules, there might be consequences.

    The situation not only applies in the situation of Runnable, but with Java 8 also very frequently in the context of Streams and other places where functional interfaces have been introduced without the possibility to deal with checked exceptions. For example, Consumer, Supplier, Function, BiFunction and so on have all been declared without facilities to deal with checked exceptions.

    So what are the situations and options? In the below text, Runnable is representative of any functional interface that doesn't declare exceptions, or declares exceptions too limited for the use case at hand.

    1. You've declared Runnable somewhere yourself, and could replace Runnable with something else.
      1. Consider replacing Runnable with Callable<Void>. Basically the same thing, but allowed to throw exceptions; and has to return null in the end, which is a mild annoyance.
      2. Consider replacing Runnable with your own custom @FunctionalInterface that can throw exactly those exceptions that you want.
    2. You've used an API, and alternatives are available. For example, some Java APIs are overloaded so you could use Callable<Void> instead of Runnable.
    3. You've used an API, and there are no alternatives. In that case, you're still not out of options.
      1. You can wrap the exception in RuntimeException.
      2. You can hack the exception into a RuntimeException by using an unchecked cast.

    You can try the following. It's a bit of a hack, but sometimes a hack is what we need. Because, whether an exception should be checked or unchecked is defined by its type, but practically should actually be defined by the situation.

    @FunctionalInterface
    public interface ThrowingRunnable extends Runnable {
        @Override
        default void run() {
            try {
                tryRun();
            } catch (final Throwable t) {
                throwUnchecked(t);
            }
        }
    
        private static <E extends RuntimeException> void throwUnchecked(Throwable t) {
            throw (E) t;
        }
    
        void tryRun() throws Throwable;
    }
    

    I prefer this over new RuntimeException(t) because it has a shorter stack trace.

    You can now do:

    executorService.submit((ThrowingRunnable) () -> {throw new Exception()});
    

    Disclaimer: The ability to perform unchecked casts in this way might actually be removed in future versions of Java, when generics type information is processed not only at compile time, but also at runtime.

    0 讨论(0)
  • 2020-12-07 15:36

    If run() threw a checked exception, what would catch it? There's no way for you to enclose that run() call in a handler, since you don't write the code that invokes it.

    You can catch your checked exception in the run() method, and throw an unchekced exception (i.e., RuntimeException) in its place. This will terminate the thread with a stack trace; perhaps that's what you're after.

    If instead you want your run() method to report the error somewhere, then you can just provide a callback method for the run() method's catch block to call; that method could store the exception object somewhere, and then your interested thread could find the object in that location.

    0 讨论(0)
  • 2020-12-07 15:38

    I think a listener pattern might help you with this scenario. In case of an exception happening in your run() method, use a try-catch block and in the catch send a notification of an exception event. And then handle your notification event. I think this would be a cleaner approach. This SO link gives you a helpful pointer to that direction.

    0 讨论(0)
提交回复
热议问题