Open In App
Related Articles

Java | Exception Handling | Question 4

Improve Article
Improve
Save Article
Save
Like Article
Like




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"); 
    }
  }


(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"); } 


Quiz of this Question

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads