Java Magazine, July/August 2018
Throwable Exception Error RuntimeException CustomException Figure 1 Class hierarchy ORACLE COM JAVAMAGAZINE JULY AUGUST 2018 82 fix this of course the source code must do something about the problem The options available in the do something category are to catch the exception or to declare that the method throws the exception This suggests that the catch block that prints the message catch would be invoked to handle the problem of the ArithmeticException that will arise from the division by zero because ArithmeticException is a RuntimeException But here is where the trouble starts That catch block then attempts to throw a Custom Exception Given that there is no related catch block for that CustomException its not thrown in a try block anyway the CustomException will propagate out of the method CustomException is a subclass of Exception not a subclass of Error These two classes are siblings in the class hierarchy as shown in Figure 1 The divide method declares that it throws Error not CustomException and because CustomException is a checked exception the declare or handle rule has been broken and the code does not compile Given this the correct answer is option D and options A B and C are all incorrect because if the code doesnt compile it surely doesnt generate any output To cure the problem a likely approach would be to change the two occurrences of Error one in the throw clause of the divide method and the other in the catch block of the main method to CustomException The code in its current form never actually throws an Error and Error is not a
You must have JavaScript enabled to view digital editions.