1

I want to call a method in java which blocks for some reason. I want to wait for the method for X minutes and then I want to stop that method.

I have read one solution here on StackOverflow which gave me a first quick start. I am writing that here :-

ExecutorService executor = Executors.newCachedThreadPool();
    Callable<Object> task = new Callable<Object>() {
       public Object call() {
          return something.blockingMethod();
       }
    };
    Future<Object> future = executor.submit(task);
    try {
       Object result = future.get(5, TimeUnit.SECONDS); 
    } catch (TimeoutException ex) {
       // handle the timeout
    } catch (InterruptedException e) {
       // handle the interrupts
    } catch (ExecutionException e) {
       // handle other exceptions
    } finally {
       future.cancel(); // may or may not desire this
    }

But now my problem is, my function can throw some Exception which I have to catch and do some task accordingly. So if in code the function blockingMethod() thorws some exception how do I catch them in Outer class ?

3
  • I would catch it in the anoymous class. This way there is no exception to catch. Commented Jul 6, 2012 at 13:15
  • Also, don't explicitly catch each and every checked exception because you're probably not going to write any specific handling code. Just write catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } Commented Jul 6, 2012 at 13:24
  • Yes Earlier I thought same thing, I would have done that but there are lots of variable of outer class which I wanted to use in exception handling code. And I am not able to access those variables in Inner class. Commented Jul 6, 2012 at 13:32

4 Answers 4

4

You have everything set up to do that in the code you provide. Just replace

// handle other exceptions

with your exception handling.
If you need to get your specific Exception you get it with:

Throwable t = e.getCause();

And to differentiate between your Exceptions you can do like this:

if (t instanceof MyException1) {
  ...
} else if (t instanceof MyException2) {
  ...
...
Sign up to request clarification or add additional context in comments.

1 Comment

I did not know that I will do that for sure :) Just one more question, if I have different types of Exception for Example MyException1, MyException2 and I have to different treatments according to different Exception. How do I differentiate between multiple Exceptions ?
1

In cause of ExecutionException instance, I suppose.

Comments

1

In the ExecutionException catch block: e.getCause()

https://docs.oracle.com/javase/6/docs/api/java/lang/Throwable.html#getCause

Comments

-1

thread.sleep(x millisecods) will stop the thread for x milliseconds, then it will resume. The other way to do it is to call thread.wait(x) (with a timeout value for x) and then call thread.notify() to "wake" the sleeping thread.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.