Skip to content
Related Articles
Open in App
Not now

Related Articles

Java | Exception Handling | Question 8

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021
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
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!