3

In java 7 we can catch multiple exceptions at a time like

try {  
    Class a = Class.forName("wrongClassName");  
    Object instance = a.newInstance();  
} catch (ClassNotFoundException | IllegalAccessException |  
   InstantiationException ex) {  
   System.out.println("Failed to create instance");  
}  

Is this Bitwise Inclusive OR? Bitwise operators are used to compare binaries as far as I know in java. If it is not, then how java differentiates this operator with Bitwise Inclusive OR??

Just want to know the name of the operator used here and is this operator exists before java 7.

Any answer is appreciated. Thanks.

4

4 回答 4

5

It's valid since Java 7, and I call it a pipe.

The catch block itself is called a multi-catch block.

Depending on the context where it's used, this operator is a bitwise or, or a multi-catch operator. Just like in (1 + 1) the + is the addition operator, and in "hello" + "world", the + is the concatenation operator.

于 2013-02-02T08:51:02.050 回答
4

The docs said:

The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

This operator used as a bitwise inclusive OR before Java 7.

于 2013-02-02T08:49:57.143 回答
1

It is unnamed (in this context)

An exception parameter may denote its type as either a single class type or a union of two or more class types (called alternatives). The alternatives of a union are syntactically separated by |.

Reference: Java Language Spec, Chapter 14.20

于 2013-02-02T09:40:11.937 回答
0

The operator exists as a (non short-circuit) OR operator before Java 7, which is probably why it was used here - you're catching one exception, OR the other, OR the other, etc.

于 2013-02-02T08:54:56.660 回答