0

This is my code where i have problem:

try {
    publisher.publish(payload).get();
} catch (com.google.api.gax.rpc.DeadlineExceededException e) {
    LOGGER.error("com.google.api.gax.rpc.DeadlineExceededException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.NotFoundException e) {
    LOGGER.error("com.google.api.gax.rpc.NotFoundException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.ApiException e) {
    LOGGER.error("com.google.api.gax.rpc.ApiException occured: " + e.getCause());
} catch (io.grpc.StatusRuntimeException e) {
    LOGGER.error("io.grpc.StatusRuntimeException occured: " + e.getCause());
} catch (java.lang.RuntimeException e) {
    LOGGER.error("RuntimeException occured: " + e.getCause());
} catch (java.util.concurrent.ExecutionException e) {
    LOGGER.error("java.util.concurrent.ExecutionException occured: " + e.getCause()); // my program cursor always come to this point
} catch (Exception e) {
    LOGGER.error("Exception occured: " + e.getCause());
}

And the getCause of ExecutionException catch is:

java.util.concurrent.ExecutionException occured: com.google.api.gax.rpc.NotFoundException: io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found

If you see I already put the catch for "com.google.api.gax.rpc.NotFoundException" in second catch, so why it went into the ExcutionException catch.

Due to this nature I am not able to write proper message for the client to address.

Thanks in advance if anyone can help.

Indrajeet Gour
  • 4,020
  • 5
  • 43
  • 70

1 Answers1

0

When publisher.publish(payload).get() would have been executed it would have thrown java.util.concurrent.ExecutionException ex= new com.google.api.gax.rpc.NotFoundException() i.e reference of java.util.concurrent.ExecutionException but object of com.google.api.gax.rpc.NotFoundException. So in try catch , when reference was checking which catch block to go, it could not get through api.gax.rpc.NotFoundException() as cactch only catches same or super class so it got catch later but since object was madeup of com.google.api.gax.rpc.NotFoundException so getCause got overridden and when you executed it object method was called.

Resolution: eg

try{

}
catch(Exception e){
            throw (e.getClass()).cast(e);

}

where you can put this catch block in java.util.concurrent.ExecutionException.

  • Ok I can understand, but now how i can make NotFoundException thrown from ExceutionException, Is there any proper way to deal this kind of scenarios. – Indrajeet Gour Dec 12 '19 at 19:13
  • catch (Exception e) { Exception exception=(e.getClass()).cast(e); throw exception; } . try this – Divanshu Aggarwal Dec 13 '19 at 04:43
  • what this will do, I wanted to capture NotFoundException and throw only NotFoundException not Exception only. please correct me. thanks – Indrajeet Gour Dec 13 '19 at 14:41
  • As we thrown Exception only, it went into Exception catch block on the parent class, it is not the intended behavior i want. – Indrajeet Gour Dec 14 '19 at 12:31
  • I did have used the same, but again it went into Exception class only, I check after using: throw (e.getClass()).cast(e); it gives me object of exception class only(i.e. ExecutionException). So how it will go into parent class NotFoundException catch block. – Indrajeet Gour Dec 15 '19 at 07:32