Order in exception handler with multiple catch

I know that as of Java 7 you can use multi-catch

, but I'm wondering if the order of exceptions in it is like in previous versions of java? For example, I introduced an exception and then SQLException

and IOException

?

try {
      // execute code that may throw 1 of the 3 exceptions below.

} catch(Exception | SQLException | IOException e) {   
    logger.log(e);

}

      

Or should I do it this way?

try {

    // execute code that may throw 1 of the 3 exceptions below.

} catch(SQLException | IOException e) {
    logger.log(e);

} catch(Exception e) {
    logger.severe(e);
}

      

+3


source to share


6 answers


There is no point in one catch block for catch(Exception | SQLException | IOException e)

, as it Exception

already covers its subclasses IOException

and SQLException

.

Therefore it catch(Exception e)

will suffice if you want all those exception types to be the same.



If you need different handling for a more general one Exception

, your second piece of code makes sense, and here the order of the two catch blocks matters, since you must catch the more specific types of exceptions first.

+4


source


Yes Order is important, from Child to Parent.

See this for more.

The exception variable is implicitly final, so we cannot assign the variable to a different value in the catch block. For example, the following piece of code will give a compilation error

} catch (IOException | SQLException ex) {

    ex = new SQLException();

}

      

The compiler will throw this error: the multi catch parameter cannot be assigned



Two or more exceptions of the same hierarchy cannot be specified in multiple catch output. For example, the following code snippet will give a compilation error because FileNotFoundException is a subtype of IOException

} catch (FileNotFoundException | IOException ex) {

    LOGGER.log(ex);

}

      

The compiler will throw this error (regardless of order): Alternatives in a multiple catch statement cannot be subclassed

The Exception class is a supertype of all exceptions, so we also cannot write

} catch (IOException | Exception ex) {

    LOGGER.log(ex);

}

      

+2


source


The multi catch function is provided in java to remove code duplication in two different hierarchical exceptions. If you use it for this reason, the order does not matter. If you are catching the parent class of the exception Exception

in a multiple catch block, then there is no need to add child exceptions IOException, SQLException

.

+1


source


Multiple instances of Exceptiontypes are separated by the "OR" character, so no, order doesn't matter.

You should use multicard if you plan on all Exceptiontypes processes to be handled the same, and if so, the order doesn't matter.

EDIT: Indeed, if the types are in a hierarchical line, only the "alternative" (in this case, the generic exception type) should be caught. However, this has nothing to do with their order.

0


source


The order matters because if you try to catch the Exception first and your second catch is for the IOException, obviously you will never hit the second catch. Thus, the order should be from smallest Exception to largest.

0


source


Exceptions have a certain hierarchy. Exception e

is more objective than others, because of this it should be the last exception you handle.

There is no comparison between IOException

and SQLException

, because of this, you can treat them however you want.

So the order should be:

try {

// execute code that may throw 1 of the 3 exceptions below.

} catch(SQLException | IOException e) {
     logger.log(e);

} catch(Exception e) {
     logger.severe(e);
}

      

or

try {

// execute code that may throw 1 of the 3 exceptions below.

} catch(SQLException e) {
     logger.log(e);

} catch(IOException e){
     logger.log(e);

} catch(Exception e) {
     logger.severe(e);
}

      

or

try {

// execute code that may throw 1 of the 3 exceptions below.

} catch(IOException e) {
     logger.log(e);

} catch(SQLException e){
     logger.log(e);

} catch(Exception e) {
     logger.severe(e);
}

      

0


source







All Articles