0
public void run() {
     final V value;
     try {
       value = getDone(future);
     } catch (ExecutionException e) {
       callback.onFailure(e.getCause());
       return;
     } catch (RuntimeException | Error e) {
       callback.onFailure(e);
       return;
     }
     callback.onSuccess(value);
   }

I think the "|" computation is a kind of bit computation, and it can be applied to byte, int, long and so on. But what's the meaning when "|" symbol applied to some java classes?

RuntimeException | Error e
lai nan
  • 61
  • 1
  • 1
  • 4

2 Answers2

0

It is to catch multiple exceptions inside a single catch block

try{
}
catch(Exception1 | Exception2 | Exception2 e){
   callSomething()
}

equivalent to :

try{
}
catch(Exception1 e){
   callSomething()
}
catch(Exception2 e){
   callSomething()
}
catch(Exception3){
   callSomething()
}
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • Does it work the same way as the logical OR (||) would work in an If statement or is it a different mechanism? – SirHawrk Aug 26 '21 at 06:27
  • 1
    it is same as the logical OR – Nilanka Manoj Aug 26 '21 at 06:28
  • Thanks a lot. Can I ask another question ? Either Exception1 or Exception2 is a java class, why can "|" symbol be applied to a java class? – lai nan Aug 26 '21 at 06:29
  • @lainan it is a simple OR statement. So what the expression means Try{....} catch(Exception1 OR Exception2). The Exceptions are defined by there respective classes – SirHawrk Aug 26 '21 at 06:32
  • these exception classes are not overlapping each other. that is why the operator is used. In the normal operator OR(||) both can be TRUE. In here class is either E1 or E2. For classes overlap represents the parent class. – Nilanka Manoj Aug 26 '21 at 06:32
0

this

catch (RuntimeException | Error e) {

means you are actually defining a block for catching either a RuntimeException OR an Error

that was introduced since java7

here more info about it: https://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html


the explained pretty well

Consider the following example, which contains duplicate code in each of the catch blocks:

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

In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.

The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:

catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97