The following program always outputs "Error:double 10.2".
I do not understand why. According to me, if fun1() allows only int to be thrown, the program should either (1) crash (2) or change the double to an int and then throw. This means, the output should be "Error:int 10". This is not the case, however. Can anyone explain ??
void fun1() throw (int)
{
cout<<"3";
throw 10.2;
cout<<"4";
}
int main()
{
try { fun1(); }
catch(int i) { cout<<"Error:int" <<i <<endl;}
catch(double i) { cout << "Error:double" << i << endl; }
cout << endl;
return 0;
}
Your compiler isn't standard compliant. According to standard your program should end with calling std::unexpected
after letting double
exception escape fun1
.
That said - don't use exception specifications. They are deprecated and useless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With