Open In App

Java | Exception Handling | Question 8

Last Updated : 28 Jun, 2021
Like Article
Like
Save
Share
Report

Predict the output of the following program.




class Test
{   int count = 0;
  
    void A() throws Exception
    {
        try
        {
            count++;
              
            try
            {
                count++;
  
                try
                {
                    count++;
                    throw new Exception();
  
                }
                  
                catch(Exception ex)
                {
                    count++;
                    throw new Exception();
                }
            }
              
            catch(Exception ex)
            {
                count++;
            }
        }
          
        catch(Exception ex)
        {
            count++;
        }
  
    }
  
    void display()
    {
        System.out.println(count);
    }
  
    public static void main(String[] args) throws Exception
    {
        Test obj = new Test();
        obj.A();
        obj.display();
    }
}


(A) 4
(B) 5
(C) 6
(D) Compilation error


Answer: (B)

Explanation: ‘throw’ keyword is used to explicitly throw an exception.
In third try block, exception is thrown. So, control goes in catch block.
Again, in catch block exception is thrown. So, control goes in inner catch block.

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads