Java Program to Handle Divide By Zero and Multiple Exceptions
Exceptions These are the events that occur due to the programmer error or machine error which causes a disturbance in the normal flow of execution of the program.
Handling Multiple exceptions: There are two methods to handle multiple exceptions in java.
- Using a Single try-catch block try statement allows you to define a block of code to be tested for errors, and we can give exception objects to the catch blow because this all the exceptions inherited by the Exception class.
- The second method is to create individual catch blocks for the different exception handler.
Hierarchy of the exceptions:
Divide by zero: This Program throw Arithmetic exception because of due any number divide by 0 is undefined in Mathematics.
Handling of Divide by zero exception: Using try-Catch Block
Multiple Exceptions (ArithmeticException and IndexoutOfBound Exception)
- Combination of two Exception using the | operator is allowed in Java.
- As soon as the first exception occurs it gets thrown at catch block.
- Check of expression is done by precedence compiler check rule from right to left of the expression.
Explanation: Here combination of ArrayIndexOutOfBounds and Arithmetic exception occur, but only Arithmetic exception is thrown, Why?
According to the precedence compiler check number=30/0 from right to left. That’s why 30/0 to throw ArithmeticException object and the handler of this exception executes Zero cannot divide any number.
Another Method of Multiple Exception: we can combine two Exception using the | operator and either one of them executes according to the exception occurs.
Please Login to comment...