It’s relatively common for a try
block to be followed by several catch
blocks to handle various types of exceptions. If the bodies of several catch
blocks are identical, you can use the multi-catch
feature (introduced in Java SE 7) to catch those exception types in a single catch
handler and perform the same task. The syntax for a multi-catch is:
catch (Type1 | Type2 | Type3 e)
Each exception type is separated from the next with a vertical bar (|
). The preceding line of code indicates that any of the types (or their subclasses) can be caught in the exception handler. Any number of Throwable
types can be specified in a multi-catch
.