Search code examples
javaerror-handlingexecutionexception

how to handle java.util.concurrent.ExecutionException exception?


Some part of my code is throwing java.util.concurrent.ExecutionException exception. How can I handle this? Can I use throws clause? I am a bit new to java.


Solution

  • If you're looking to handle the exception, things are pretty straightforward.

       public void exceptionFix1() {
           try {
               //code that throws the exception
           } catch (ExecutionException e) {
               //what to do when it throws the exception
           }
       }
    
       public void exceptionFix2() throws ExecutionException {
           //code that throws the exception
       }
    

    Keep in mind that the second example will have to be enclosed in a try-catch block somewhere up your execution hierarchy.

    If you're looking to fix the exception, we'll have to see more of your code.