Capturing specific exceptions allows you to tailor specific answers for each case.
At the logical level, a series of catch blocks is the same as having one catch block, and then writing your own conditional logic inside a single catch block. Note that conditional logic should also indicate the exception as specific subtypes if you want to access the detailed information declared in the subtype.
A few drawbacks of catching each exception separately include the entire try-catch structure, which becomes very large and makes the logic of the containing method more complex, and the need to repeat the code in many or all of the individual catch blocks (for example, registering an exception).
In some cases, the complexity of some basic API ensures that all different exceptions are processed and the try-catch structure is retrieved into the utility method. For example, invoking a method through reflection appears to regularly require facade APIs.
At the API design level, there is always a balance between
- Very rich (public) exception hierarchy
- Including error codes as part of the information contained in the base exception, and
- Open set of marker interfaces and use of private subtypes of exceptions
Dilum rananunga
source share