Only the First Matching catch Executes

If multiple catch blocks match a particular exception type, only the first matching catch block executes when an exception of that type occurs. It’s a compilation error to catch the exact same type in two different catch blocks associated with a particular try block. However, there can be several catch blocks that match an exception—i.e., several catch blocks whose types are the same as the exception type or a superclass of that type. For instance, we could follow a catch block for type ArithmeticException with a catch block for type Exception—both would match ArithmeticExceptions, but only the first matching catch block would execute.

Common Programming Error 11.3

Placing a catch block for a superclass ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.