Open In App
Related Articles

Java | Exception Handling | Question 7

Improve Article
Improve
Save Article
Save
Like Article
Like

Predict the output of the following program.




class Test
{
    String str = "a";
  
    void A()
    {
        try
        {
            str +="b";
            B();
        }
        catch (Exception e)
        {
            str += "c";
        }
    }
  
    void B() throws Exception
    {
        try
        {
            str += "d";
            C();
        }
        catch(Exception e)
        {
            throw new Exception();
        }
        finally
        {
            str += "e";
        }
  
        str += "f";
  
    }
      
    void C() throws Exception
    {
        throw new Exception();
    }
  
    void display()
    {
        System.out.println(str);
    }
  
    public static void main(String[] args)
    {
        Test object = new Test();
        object.A();
        object.display();
    }
  
}


(A) abdef
(B) abdec
(C) abdefc


Answer: (B)

Explanation: ‘throw’ keyword is used to explicitly throw an exception.
finally block is always executed even when an exception occurs.
Call to method C() throws an exception. Thus, control goes in catch block of method B() which again throws an exception. So, control goes in catch block of method A().

Quiz of this Question
Please comment below if you find anything wrong in the above post


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
Previous
Next
Similar Reads