2

(context: integrating with an application -Jenkins- with various plugins not all with good design)

I have these exception:

public class AbortException extends IOException

and in some cases I want to catch IOException but avoid catching the AbortException

Which one is more correct?

try {
...
} catch (AbortException e) {
    throw e //this failure should fail
} catch (IOException e) {
    [handle this]
}

or

try {
...
} catch (IOException e) {
    if (e instanceof AbortException)
        throw e //this failure should fail
    else
        [handle this]
}

I am unsure if in the first case the second catch block would intercept the re-thrown AbortException

  • 2
    Both will do what you're looking for, but the first one is the native way to do it. The second catch block won't catch anything that is not in the `try` block. – ernest_k Feb 12 '21 at 05:50
  • 1
    Thanks @ernest_k, I actually wrote a quick test and yes, they both do it, so I'll keep #1 as it's easier to read – Vincenzo Melandri Feb 12 '21 at 06:25

0 Answers0