Knowee
Questions
Features
Study Tools

What is the output of the following Java program?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"); } }}

Question

What is the output of the following Java program?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"); } }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the Java program will be: "Caught base class exception"

In Java, the catch blocks are examined in the order they appear. The first catch block that is capable of handling the exception is the one that gets executed. In this case, since the Derived class extends the Base class, the catch block for Base is capable of handling the exception of type Derived. Therefore, the catch block for Base is the one that gets executed, and "Caught base class exception" is printed to the console. The catch block for Derived is never reached.

This problem has been solved

Similar Questions

What will be the output of the following Java program?class exception_handling { public static void main(String args[]) { try { System.out.print("Hello" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } }a) Hellob) Worldc) HelloWorldd) Hello World

What is the Output of following Java Program?abstract class Demo{    public int a;    Demo()    {        a = 10;    }     abstract public void set(); }class Test extends Demo{     final public void get()    {        System.out.println("a = " + a);    }     public static void main(String[] args)    {        Test obj = new Test();        obj.get();    }}Select one:a.Compile Time Errorb.a=10c.Runtime Exception

What will be the output of the following code?This question is required.*class Base {public void display() {System.out.println("Base");}}class Derived extends Base {public void display() {System.out.println("Derived");}}public class Main {public static void main(String[] args) {Base b = new Derived();b.display();}}

Select the correct answerWhat will be the output of the following Java program? final class base     {         int a;    }        class derived extends base    {        int b;        System.out.println(b + " " + a);      }        class inheritance     {        public static void main(String args[])        {            derived obj = new derived();            obj.display();             }   }OptionsRuntime Error3 32 2Compilation Error

What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }2 23 3Runtime ErrorCompilation Erro

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.