What is the best way to handle an ExecutionException?

后端 未结 11 1696
遇见更好的自我
遇见更好的自我 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 13:02

    Here are couple of interesting information for checked and against Checked Exceptions. Brian Goetz discussion and an argument of against checked exceptions from Eckel Discussion. But I did not know if you have already implemented and given a thought about the checked exception refactor that is discussed by Joshua in this book.

    According the Effective Java pearls, one of the preferred method of handling Checked exceptions is to turn a checked exception into an Un-Checked Exception. So for example,

    try{
    obj.someAction()
    }catch(CheckedException excep){
    }
    

    change this implementation to

    if(obj.canThisOperationBeperformed){
    obj.someAction()
    }else{
    // Handle the required Exception.
    }
    

提交回复
热议问题