Handling Multiple Unrelated Exceptions When are multiple unrelated exceptions to be caught, separate catch blocks are required. In this case the parameter of each block must be unique; otherwise this would give rise to syntax error. The order in which the catch blocks are arranged does not matter. Listing 3.8 shows a class called Division. The critical point in this class is the arithmetic expression, arr[i]/arr[i+1] on Line 14. The two critical issues to look for are an index that could – exceed the the limit of the array; and a denominator whose value could be zero. Against this background it would be appropriate to apply the exception handling technique to prevent the program from terminating abruptly when it is executing. The exception objects as you see are ArithmeticException and ArrayIndexOutOfBoundsException. These objects are not related; hence the order of specifying them does not make any difference to the result. The test class MyException, shown in Listing 3.9, creates an array of three integer values. See Line 5. It uses that array to create an object from the class Division, as shown in Line 7. The calls the method divide, using the index value i, as parameter.
Handling Multiple Unrelated Exceptions When are multiple unrelated exceptions to be caught, separate catch blocks are required. In this case the parameter of each block must be unique; otherwise this would give rise to syntax error. The order in which the catch blocks are arranged does not matter. Listing 3.8 shows a class called Division. The critical point in this class is the arithmetic expression, arr[i]/arr[i+1] on Line 14. The two critical issues to look for are – an index that could exceed the the limit of the array; and a denominator whose value could be zero. Against this background it would be appropriate to apply the exception handling technique to prevent the program from terminating abruptly when it is executing. The exception objects as you see are ArithmeticException and ArrayIndexOutOfBoundsException. These objects are not related; hence the order of specifying them does not make any difference to the result. The test class MyException, shown in Listing 3.9, creates an array of three integer values. See Line 5. It uses that array to create an object from the class Division, as shown in Line 7. The calls the method divide, using the index value i, as parameter.
chapter.3.1.docx