Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception handling in C++: Throwing a double when using "throw(int)"

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;
}
like image 330
user1414696 Avatar asked Jun 20 '13 13:06

user1414696


1 Answers

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.

like image 74
Tadeusz Kopec for Ukraine Avatar answered Oct 16 '22 09:10

Tadeusz Kopec for Ukraine