What is the best way to handle an ExecutionException?

后端 未结 11 1698
遇见更好的自我
遇见更好的自我 2020-12-04 12:07

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 workin

11条回答
  •  时光取名叫无心
    2020-12-04 12:55

    I'm not sure why you have the if/else block in the catch and instanceof, I think you can do what you want with:-

    catch( ProcessExecutionException ex )
    {
       // handle ProcessExecutionException
    }
    catch( InterruptException ex )
    {
       // handler InterruptException*
    }
    

    One thing to consider, to reduce clutter, is to catch the exception inside your callable method and re-throw as your own domain/package specific exception or exceptions. How many exceptions you need to create would largely depend on how your calling code will respond to the exception.

提交回复
热议问题