title: What is the best way to handle an ExecutionException?
body: <p>I have a method that performs some task with a timeout. I use the ExecutorServer.submit() to get a Future object, and then I call future.get() with a timeout. This is working fine, but my question is the best way to handle checked exceptions that can be thrown by my task. The following code works, and preserves the checked exceptions, but it seems extremely clumsy and prone to break if the list of checked exceptions in the method signature changes.</p>
  
  <p>Any suggestions on how to fix this? I need to target Java 5, but I'd also be curious to know if there are good solutions in newer versions of Java.</p>
  
  <pre><code>public static byte[] doSomethingWithTimeout( int timeout ) throws ProcessExecutionException, InterruptedException, IOException, TimeoutException {
  
      Callable&lt;byte[]&gt; callable = new Callable&lt;byte[]&gt;() {
          public byte[] call() throws IOException, InterruptedException, ProcessExecutionException {
              //Do some work that could throw one of these exceptions
              return null;
          }
      };
  
      try {
          ExecutorService service = Executors.newSingleThreadExecutor();
          try {
              Future&lt;byte[]&gt; future = service.submit( callable );
              return future.get( timeout, TimeUnit.MILLISECONDS );
          } finally {
              service.shutdown();
          }
      } catch( Throwable t ) { //Exception handling of nested exceptions is painfully clumsy in Java
          if( t instanceof ExecutionException ) {
              t = t.getCause();
          }
          if( t instanceof ProcessExecutionException ) {
              throw (ProcessExecutionException)t;
          } else if( t instanceof InterruptedException ) {
              throw (InterruptedException)t;
          } else if( t instanceof IOException ) {
              throw (IOException)t;
          } else if( t instanceof TimeoutException ) {
              throw (TimeoutException)t;
          } else if( t instanceof Error ) {
              throw (Error)t;
          } else if( t instanceof RuntimeException) {
              throw (RuntimeException)t;
          } else {
              throw new RuntimeException( t );
          }
      }
  }
  </code></pre>
  
  <p>=== UPDATE ===</p>
  
  <p>Many people posted responses that recommended either 1) re-throwing as a general exception, or 2) re-throw as an unchecked exception. I don't want to do either of these, because these exception types (ProcessExecutionException, InterruptedException, IOException, TimeoutException) are important - they will each be handled differently by the calling processed. If I were not needing a timeout feature, then I would want my method to throw these 4 specific exception types (well, except for TimeoutException). I don't think that adding a timeout feature should change my method signature to throw a generic Exception type.</p>