Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Java | Exception Handling | Question 8

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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


My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads