Catch handler - Features,Definition - Exception Handling

Q.  Which of the following statements are true about Catch handler?

1. It must be placed immediately after try block T.
2. It can have multiple parameters.
3. There must be only one catch handler for every try block.
4. There can be multiple catch handler for a try block T.
5. Generic catch handler can be placed anywhere after try block.

- Published on 17 Jul 15

a. Only 1, 4, 5
b. Only 1, 2, 3
c. Only 1, 4
d. Only 1, 2

ANSWER: Only 1, 4
 

    Discussion

  • Ramesh   -Posted on 24 Oct 15
    catch handler must be placed immediately after try block.
    If you write some line of code after try block, then it will give compile tile error.
    There can be multiple catch handlers for a try block.
    Example:
    class Handlers
    {
    public:
    void Test()
    {
    int num;
    string str = "you have entered wrong number";
    cout << "Please input 1 or 2: ";
    cin >> num;

    try
    {
    if ( num == 1 )
    {
    throw 50;
    }
    if ( num == 2 )
    {
    throw 10.1f;
    }
    if ( num != 1 || num != 2 )
    {
    throw str;
    }
    }

    catch (int a)
    {
    cout << "An exception occurred!" << endl;
    cout << "Exception number is: " << a << endl;
    }
    catch (float b)
    {
    cout << "An exception occurred!" << endl;
    cout << "Exception number is: " << b << endl;
    }
    catch (...)
    {
    cout << "A generic handler called!" << endl;
    cout << str << endl;
    }
    }
    };
    int main ()
    {
    clrscr();
    Handlers obj;
    obj.Test();
    getch();
    return 0;
    }

Post your comment / Share knowledge


Enter the code shown above:
 
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)