adplus-dvertising
frame-decoration

Question

What is the output of this program?
    #include <iostream>
    using namespace std;
    void PrintSequence(int StopNum)
    {
        int Num;
        Num = 1;
        while (true) 
        {
            if (Num >= StopNum)
                throw Num;
            cout << Num;
            Num++;
        }
    }
    int main(void)
    {
        try 
        {
            PrintSequence(20);
        }
        catch(int ExNum)
        {
            cout << "Caught an exception with value: " << ExNum;
        }
        return 0;
    }

a.

compile time error

b.

prints first 19 numbers

c.

prints first 19 numbers and throws exception at 20

d.

none of the mentioned

Answer: (c).prints first 19 numbers and throws exception at 20

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of this program?