Johnyy
Johnyy

Reputation: 2116

java ScheduledExecutorService runnable exception handling

I am realizing that if a exception are raised inside(or not, but should be related to) my runnable's run method, all my future tasks will not be run.

So my question is: How can I recover from such a exception (where to catch it)?

I have tried this: ScheduledExecutorService Exception handling If i do a while loop to catch the exception, the future tasks are still not executed. I also tried to schedule the catch, no help either.

I tried to put a huge try/catch to wrap all the code in run method but it seems to be not catching anything, and some exception are still not catches and causing all my future tasks to not run.

Upvotes: 0

Views: 1001

Answers (1)

cheframzi
cheframzi

Reputation: 156

In the executor framework, you are giving control of running a job away from one main application thread to a thread pool thread. A thread submits the work through a schedule, or submit method is returned a Future object that allows it to get information through a get method. The get method will throw an executor exception whose cause is probably the exception that your code inside the runnable threw. If the main thread does not do that it will never see that exception, so it really depends on your application logic flow.

Another thing also to mention, if you try catch all, what do you mean by that if you are doing something similar to

try {
....
}
catch(Exception e) {
    .... }

you are really not catching errors in your app (throwable is the father of Exception and Error) so you might have some static initializer error (an exception caught in a static block)

Again it all depends on how you want exception handling to happen you have full power,

Thank you

Upvotes: 2

Related Questions