SwissFr
0
Q:

java catch multiple exceptions


catch(IOException | SQLException ex){
     logger.error(ex);
     throw new MyException(ex.getMessage());
}
9
public class MyClass implements MyInterface {
  public void find(int x) throws A_Exception, B_Exception{
    ----
    ----
    ---
  }
}
3
// syntax in java 7 Java catch multiple exceptions and rethrow exception
try
{
   // code that throw exceptions 1 and 3
}
catch(SQLException | IOException e)
{
   logger.log(e);
}
catch(Exception e)
{
   logger.severe(e);
}
1
// Before java 7 - syntax Java catch multiple exceptions and rethrow exception
try
{
   // code that throw exceptions 1 and 3
}
catch(SQLException e)
{
   logger.log(e);
}
catch(IOException e)
{
   logger.log(e);
}
catch(Exception e)
{
   logger.severe(e);
}
1
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
0

New to Communities?

Join the community