Catch with multiple parameters

First I found in cplusplus.com the following quote:

The catch format is similar to a regular function that always has at least one parameter.

But I tried this:

try
{
    int kk3,k4;
    kk3=3;
    k4=2;
    throw (kk3,"hello");
}
catch (int param)
{
    cout << "int exception"<<param<<endl;     
}
catch (int param,string s)
{
    cout<<param<<s;
}
catch (char param)
{
    cout << "char exception";
}
catch (...)
{
    cout << "default exception";
}

The compiler doesn't complain about the throw with braces and multiple arguments. But it actually complains about the catch with multiple parameters in spite of what the reference said. I'm confused. Does try and catch allow this multiplicity or not? And what if I wanted to throw an exception that includes more than one variable with or without the same type.

Answers


(kk3, "hello") is a comma expression. The comma expression evaluates all of its arguments from left to write and the result is the rightmost argument. So in the expression

int i = (1,3,4); 

i becomes 4.

If you really want to throw both of them (for some reason) you could throw like this

 throw std::make_pair(kk3, std::string("hello")); 

and catch like this:

catch(std::pair<int, std::string>& exc)
{
}

And a catch clause has exactly one argument or

...

HTH


Need Your Help