Java Catch Multiple Exceptions

While dealing with complex functionalities in Java, there can be instances where the programmer needs to refrain from the probable limitations. For instance, implementing and analyzing the algorithms based on the likely exceptions. In such situations, catching “multiple exceptions” in Java enables the developer to implement the logic appropriately and streamline the code execution without any faced bottlenecks.

This blog will demonstrate the approaches to catching multiple exceptions in Java.

How to Catch Multiple Exceptions in Java?

The “try…catch” statements can be utilized to catch multiple exceptions in Java.

Example 1: Catch Multiple Exceptions in Java Separately

In this example, multiple exceptions can be coped with by specifying them separately:

try {  
int x = Integer.parseInt(null);
System.out.println(x);
int a = 2;
int b = a/0;
System.out.println(b);
}
catch (NumberFormatException f) {
 System.out.println(f.getMessage());
}
catch (ArithmeticException e) {
 System.out.println(e.getMessage());
}

 

In the above code snippet:

  • First, include the “try” block.
  • First Exception: Apply the “parseInt()” method to transform the null string into an integer resulting in the “NumberFormatException”.
  • Second Exception: In this exception, divide the initialized number by “0” so the infinity is returned, leading to the “ArithmeticException”.
  • In the “catch” block, cater to both faced exceptions.
  • Within these blocks, display the detailed exception message as a string via the “getMessage()” method.
  • Note that the exception faced first will be catered to formerly and displayed irrespective of the sequence of the specified “catch” blocks.

Output

In this output, it can be observed that since the parsing is done first in the code, the corresponding exception is displayed formerly.

Example 2: Catch Multiple Exceptions in Java Using Bitwise OR Operator “|”

In this specific example, multiple exceptions can be faced with the help of the bitwise OR operator “|” is also termed as a “single pipe” that will verify every part of the condition, i.e., “exception”, and return the formerly faced exception:

try {
int a = 2;
int b = a/0;
System.out.println(b);
int x = Integer.parseInt(null);
System.out.println(x);
}
catch (NumberFormatException | ArithmeticException e) {
System.out.println(e.getMessage());
}

 

In this code block:

  • Recall the discussed approaches for specifying the same exceptions that must be catered.
  • Note that the formerly faced exception, in this case, is the “ArithematicException” to clarify the concept.
  • In the “catch” block, apply the bitwise OR operator “|” to iterate through each of the specified exceptions and return the corresponding message against the formerly faced exception via the “getMessage()” method.

Output

This outcome shows that since the “ArithematicException” is faced first, the corresponding exception message is returned as a “string”.

Conclusion

The “try…catch” statements can be used to catch multiple Java exceptions. This can be achieved by specifying the exceptions separately or using the bitwise OR operator “|“. The exceptions are catered based on the sequence in which they occur. This blog discussed the methodologies to catch multiple exceptions in Java.



from https://ift.tt/gtvUWCT

Post a Comment

0 Comments