Why use bitwise or in catch blocks to handle exceptions in Java?

Why use bitwise OR here?

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

Solution

In this case, this is not a bitwise operator It is a syntax that catches multiple exceptions

Functions added in Java 7

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

Before Java 7, you need to write

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

Look, have they simplified it?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>