try {
String str = null;
int i = 12 / 0;
System.out.println("String length : " + str.length());
} catch(NullPointerException | ArithmeticException e) /*Multiple exceptions in a catch statement*/{
System.out.println("Exception occured : " + e.getMessage());
}
As seen in above code, two exceptions are mentioned in catch block and which ever exception will be occur that will be catch.
But we cannot mention exceptions which are related by subclassing in mutli catch statement.
For e.g.
try {
String str = null;
int i = 12 / 0;
System.out.println("String length : " + str.length());
} catch(NullPointerException | ArithmeticException | Exception e) /*Not allowed*/{
System.out.println("Exception occured : " + e.getMessage());
}
In above code, Exception is parent class of NullPointerException and ArithmeticException and so these cannot be mention in same multi-catch statement.