• It is possible that a program segment has more than one condition to throw an exception.
  • For these situations we can associate more than one catch statement with a try block.
  • When an exception is thrown, the exception handlers are searched in order for an appropriate match.
  • The fist handler that yields a match is executed.
  • After executing the handler, the control goes to the first statement after the lat catch block for that try.

Note: It is possible that arguments of several catch statements match the type of an exception.

         In such cases, the first handler that matches the exception type is executed.

Example:

#include<iostream>

using namespace std;

void test(int x)

{

    try
    {
        if(x==1)
        {
            throw(x);
        }
        else if(x==0)
        {
            throw 'x';
        }
        else if(x==-1)
        {
            throw 1.0;
        }
    }
    catch(char c)
    {
        cout<<"Caught a character"<<endl;
    }
    catch(int m)
    {
        cout<<"Caught an integer"<<endl;
    }
    catch(float n)
    {
        cout<<"Caught a double"<<endl;
    }
    cout<<"End of the try catch system"<<endl;
}

int main()
{
    out<<"Testing multiple catch"<<endl;
    cout<<"x==1"<<endl;
    test(1);
    cout<<"x==0"<<endl;
    test(0);
    cout<<"x==-1"<<endl;
    test(-1);
    cout<<"x==2"<<endl;
    test(2);
    return 0;
}