What is the function of the "| "Inside the catch?

7

I read that || (OR) is for boolean operations and | (Bitwise operation) for bit operations. What is its function inside a catch with multiple exceptions , then? I mean, I know it eliminates duplicate code but I've always considered it as a Boolean OR.

Example

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

If there is any IOException OR SQLException the loop is executed.

    
asked by anonymous 22.04.2015 / 20:15

4 answers

2

In java SE 7 or higher a single block of catch can deal with more than one type of exception, for this you must specify the types of exception you want to capture and separate them with a | pipe, example :

try {
    ...
}
catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
}

In this case the pipe is not a logical operator but a feature syntax.

In previous versions of Java we had to treat exceptions individually or use a more comprehensive class ( Exception , for example) to capture postings of all possible exceptions, with this feature we can transform, for example:

This code

try {
    System.out.println(10 / 0);
} catch (ArithmeticException e) {
    System.out.println("Erro: " + e.getMessage());
} catch (IllegalArgumentException e) {
    System.out.println("Erro: " + e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Erro: " + e.getMessage());
}

In this

try {
    System.out.println(10 / 0);
} catch (ArithmeticException | IllegalArgumentException | ArrayIndexOutOfBoundsException e) {
    System.out.println("Erro: " + e.getMessage());
}

NOTE: The variable declared in catch is always final , ie we can not assign any value to it.

Source: Oracle

    
22.04.2015 / 20:57
7

This is a feature that was introduced from Java 7. You can use it to remove possible snippets of duplicate catch's. Ex:

catch (IOException ex) {
     logger.log(ex);
     throw ex;
} catch (SQLException ex) {
     logger.log(ex);
     throw ex;
}

by:

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

You can see a full explanation on this link

    
22.04.2015 / 20:58
3

The symbol serves exactly what you assumed, it's like an "OU." It is for you to catch multiple exceptions in a single block catch , rather than making a catch for each exception. For example, this:

try {
  // Código a executar
} catch (ExceptionType1 name) {
  // Código a executar quando uma exceção do tipo "ExceptionType1" for disparada
} catch (ExceptionType2 name) {
  // Código a executar quando uma exceção do tipo "ExceptionType2" for disparada
}

It can be reduced to this:

try {
  // Código a executar
} catch (ExceptionType1|ExceptionType2 name) {
  // Código a executar quando uma exceção do tipo "ExceptionType1" ou "ExceptionType2" for disparada
}

But why not use the "||"?

If we analyze, | and || have the same function: check multiple conditions at once ("if condição1 is X or condição2 for Y or condição3 for Z", etc. ). But there is a small difference. The || is a logical operator , it serves as we have already said to verify several conditions in a conditional structure . But the code inside catch is not a condition, so || does not take the "or conditional" function.

Reference

22.04.2015 / 20:58
-2

Good afternoon, I do not have much experience, but I think you could put two catch ... in SQL what do you want to do? Was the connection?

catch (IOException ex1) {
    logger.log(ex);
    throw ex;
}
catch (SQLException e) {
    System.out.println("Erro ao Conectar ", e.getMessage());
}
    
22.04.2015 / 20:43