Last Updated: 10 July, 2023
In Java, it is possible to have multiple catch blocks following a try block to handle different types of exceptions. This allows you to specify different exception handlers for different types of exceptions that may occur within the try block. The catch blocks are evaluated in order, and the first catch block that matches the type of the thrown exception is executed.
Here's the syntax for using multiple catch blocks in Java::
📝 There is no limit on the number of catch blocks corresponding to a try block.
Flowchart of Multi-catch Block in Java
Syntax:
Output
Error: ArrayIndexOutOfBoundsException
Out of try-catch block...
Output
Error: ArithmeticException
Out of try-catch block...
Output
Length: 14
Error: Exception occured...
Output
Error: Exception occured...
Out of try-catch block...
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com