Class ExceptionProxy


  • @Internal
    @Deprecated
    public class ExceptionProxy
    extends Object
    Deprecated.
    A proxy that communicates exceptions between threads. Typically used if an exception from a spawned thread needs to be recognized by the "parent" (spawner) thread.

    The spawned thread would set the exception via reportError(Throwable). The parent would check (at certain points) for exceptions via checkAndThrowException(). Optionally, the parent can pass itself in the constructor to be interrupted as soon as an exception occurs.

    
     final ExceptionProxy errorProxy = new ExceptionProxy(Thread.currentThread());
    
     Thread subThread = new Thread() {
    
         public void run() {
             try {
                 doSomething();
             } catch (Throwable t) {
                 errorProxy.reportError(
             } finally {
                 doSomeCleanup();
             }
         }
     };
     subThread.start();
    
     doSomethingElse();
     errorProxy.checkAndThrowException();
    
     doSomethingMore();
     errorProxy.checkAndThrowException();
    
     try {
         subThread.join();
     } catch (InterruptedException e) {
         errorProxy.checkAndThrowException();
         // restore interrupted status, if not caused by an exception
         Thread.currentThread().interrupt();
     }
     
    • Constructor Detail

      • ExceptionProxy

        public ExceptionProxy​(@Nullable
                              Thread toInterrupt)
        Deprecated.
        Creates an exception proxy that interrupts the given thread upon report of an exception. The thread to interrupt may be null.
        Parameters:
        toInterrupt - The thread to interrupt upon an exception. May be null.
    • Method Detail

      • reportError

        public void reportError​(Throwable t)
        Deprecated.
        Sets the exception and interrupts the target thread, if no other exception has occurred so far.

        The exception is only set (and the interruption is only triggered), if no other exception was set before.

        Parameters:
        t - The exception that occurred
      • checkAndThrowException

        public void checkAndThrowException()
                                    throws Exception
        Deprecated.
        Checks whether an exception has been set via reportError(Throwable). If yes, that exception if re-thrown by this method.
        Throws:
        Exception - This method re-throws the exception, if set.