Only the First Matching catch Executes
If there are multiple catch
blocks that 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 may 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 ArithmeticException
s, but only the first matching catch
block would execute.
Get Android™ How to Program, Second 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.