Java | Exception Handling | Question 4
class Base extends Exception {} class Derived extends Base {} public class Main { public static void main(String args[]) { // some other stuff try { // Some monitored code throw new Derived(); } catch(Base b) { System.out.println("Caught base class exception"); } catch(Derived d) { System.out.println("Caught derived class exception"); } } } |
chevron_right
filter_none
(A) Caught base class exception
(B) Caught derived class exception
(C) Compiler Error because derived is not throwable
(D) Compiler Error because base class exception is caught before derived class
Answer: (D)
Explanation: See Catching base and derived classes as exceptions
Following is the error in below program
Main.java:12: error: exception Derived has already been caught
catch(Derived d) { System.out.println("Caught derived class exception"); }
Recommended Posts:
- Java | Exception Handling | Question 3
- Java | Exception Handling | Question 7
- Java | Exception Handling | Question 1
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 2
- Java | Exception Handling | Question 6
- Java | Exception Handling | Question 8
- Output of Java program | Set 12(Exception Handling)
- Exception Handling with Method Overriding in Java
- Nested try blocks in Exception Handling in Java
- C++ | Exception Handling | Question 3
- C++ | Exception Handling | Question 12
- C++ | Exception Handling | Question 6
- C++ | Exception Handling | Question 1
- C++ | Exception Handling | Question 4



