Shall we handle all exceptions in one try-catch block? code example
Example 1: Can we catch more than one exception in single catch block
From Java 7, we can catch more than one exception with single catch block.
This type of handling reduces the code duplication.
When we catch more than one exception in single catch block ,
catch parameter is implicity final. We cannot assign any value to catch
parameter.
Ex : catch(ArrayIndexOutOfBoundsException || ArithmeticException e){
20
}
In the example e is final we cannot assign any value or
modify e in catch statement
Example 2: handling exceptions
I use try & catch & finally block
to handle exception if I will use the method
in different class.
Or If I will use it only once and if it is checked
exception then I use THROWS keyword on method signature