Open In App
Related Articles

Java | Exception Handling | Question 8

Improve Article
Improve
Save Article
Save
Like Article
Like

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


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