简体   繁体   中英

why bitwise or is used here in catch block to handle the exception in java?

Why bitwise or is used here?

try  
{  
    //some errorprone code  
}  
catch(NullPointerException |NumberFormatExceptioon e)  
{  
    ////handling the exception  
}

That's not a bitwise operator in this case. It's the syntax of catching multiple exceptions.

Feature added in Java 7.

https://docs.oracle.com/javase/8/docs/technotes/guides/language/catch-multiple.html

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

Before java 7, you need write

try{
    //some errorprone code  
 }catch (NullPointerException ex) {
      //handle
} catch (NumberFormatExceptioon ex) {
     //handle
}

Look, they simplified it right ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM