What is the function of the " | " operator within the catch?

Asked

Viewed 342 times

7

I read that || (OR) is for boolean operations and | (Bitwise Operation) for bit operations. What is your function within 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.

  • 2

    I can’t answer that, but I would venture to say that it’s just a syntactic ruse, not a operator as | or ||. After all, they are not values that are being combined, but types (types even, not representations of types, such as instances of Class etc). So that the usual rules of the language for operators do not apply.

4 answers

7

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

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

for:

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

You can see a full explanation on this link

3


In java SE 7 or higher a single block of catch can handle more than one type of exception, for this you must specify the types of exception you want to capture and separate them by 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 syntax of the resource.

In previous versions of Java we had to treat the exceptions individually or use a more comprehensive class (Exception, for example) to capture launches 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());
}

OBS: The variable declared in catch is always final, that is, we cannot assign any value to it.

Source: Oracle

3

The symbol serves exactly for what you have guessed, it is like an "OR". It serves for you to capture multiple exceptions in a single block catch, instead of making a catch for every 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
}

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, the | and the || has the same function: to check multiple conditions at once ("if condição1 for X or condição2 for Y or condição3 for Z", etc). But there is a small difference. The || is a logical operator, as has already been said, we check various conditions in a conditional structure. But the code within the catch nay is a condition, therefore, the || does not take the role of "or conditional".

Reference

-2

Good afternoon, I don’t have much experience, but I think you could put two catch... in SQL what do you want to do? It would be the connection?

catch (IOException ex1) {
    logger.log(ex);
    throw ex;
}
catch (SQLException e) {
    System.out.println("Erro ao Conectar ", e.getMessage());
}
  • 12

    I don’t think you understand the question. What is being shown is valid syntax, supported since version 7, but it is correct. What the AP asked for was an explanation about it, and not an alternative to it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.