Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard
Question 53
Given code of Test.java file:
- package com.udayan.ocp;
- class MyException1 extends RuntimeException {}
- class MyException2 extends RuntimeException {}
- public class Test {
- private static void m() {
- try {
- throw new RuntimeException();
- } catch(RuntimeException ex) {
- throw new MyException1();
- } finally {
- throw new MyException2();
- }
- }
- public static void main(String[] args) {
- try {
- m();
- } catch(MyException1 e) {
- System.out.println("MyException1");
- } catch(MyException2 e) {
- System.out.println("MyException2");
- } catch (RuntimeException e) {
- System.out.println("RuntimeException");
- }
- }
- }
What will be the result of compiling and executing Test class?
- A RuntimeException
- B MyException1
- C MyException2
Reveal correct answer
Correct answer: C
Explanation
If finally block throws exception, then exception thrown by try or catch block is ignored.
In this case, method m() throws an instance of MyException2 class.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
