adplus-dvertising
frame-decoration

Question

What will be output for the following code?
class Base extends Exception {}
class Derived extends Base {}
public class Main {
 public static void main(String args[]) 
 {
  try 
  {
   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

Posted under Java Programming

Answer: (d).Compiler Error because base class exception is caught before derived class

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What will be output for the following code?

Similar Questions

Discover Related MCQs

Q. Which exception is thrown when divide by zero statement executes?

Q. In which of the following package Exception class exist?

Q. Which of these is a super class of all errors and exceptions in the Java language?

Q. Which statement is true?

Q. Which statement is true?

Q. Which statement is true?

Q. Which four can be thrown using the throw statement?

1. Error
2. Event
3. Object
4. Throwable
5. Exception
6. RuntimeException

Q. Which statement is true?

Q. Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?

Q. Which of these operator is used to generate an instance of an exception than can be thrown by using throw?

Q. Which of these class is related to all the exceptions that are explicitly thrown?

Q. Which of these keywords is used to generate an exception explicitly?

Q. What exception thrown by parseInt() method?

Q. Which of these handles the exception when no catch is used?

Q. Which of these class is related to all the exceptions that cannot be caught?

Q. Which of these class is related to all the exceptions that can be caught by using catch?

Q. Which of these is a super class of all exceptional type classes?

Q. Which of these classes is super class of Exception class?

Q. Which of these methods return localized description of an exception?

Q. Which of these methods is used to print stack trace?