Java multiple catch blocks

Posted in Exception Handling By Raj On December 26, 2017


In my previous article, I have explained about java try-catch block, in this article, I am going to cover how to use multiple catch blocks in Java exceptions handling

You can handle multiple exceptions by using multiple catch blocks, but the order is very important whatever match first will get executed, so first catch more specific ones later catch less specific or generics exceptions

Check below reading an excel file example, here, first I am catching FileNotFoundException which is more specific one then I am catching Generic Exception

What if I change the order of the catch blocks?

If the order is not correct then you will get compile time error, check below code and screenshot, the compile is throwing unreachable catch block error because it is already handled in the Exception class


Syntax :

You can group multiple exceptions in single catch block something like below