Why can you specify a type of the parameter for the throw keyword when you can throw another type anyway? An example:
class A {};
class B {};
void tryexcept() throw(B*)
{
B* b = new B();
A* a = new A();
throw (a); // It works pretty smooth?
}
Int main()
{
try {
tryexcept();
}
catch (A* style)
{
cout << "in handler for A\n";
}
catch (B* style)
{
cout << "in handler for B\n";
}
catch (...)
{
cout << "in handler for everything\n";
}
return 0;
}
Output:
in handler for A
Btw. I am using Visual Studio.
But in the function declaration, I set the parameter type of throw to B*
, so how can I throw an object of type A*
? I mean it works just as if I had declared the function like void tryexcept() throw(A*)
since the right catch block is being used.