views:

91

answers:

2

I was just playing around with exceptions in the visual studio and with the above code I was expecting that since my exception specification doesnt mention anything the bad_exception should have been thrown. But what actually happens is the exception gets caught by the apprpriate handler. Why so? Am i missing some setting or something in the IDE?

While i got stuck at above mentioned, Actually I was trying to find answer to the question,If i have a exception blank specification then what gets called? the unexpected() method or a *bad_exception* will be thrown and if both in what order? Here's the code.

 #include "stdafx.h"  
 #include <stdio.h>  
 #include <exception>  
 #include <iostream>  


using namespace std;

class A
{
    public:
        int i;
};

void myunexpected () 
{
    cerr << "unexpected called\n";
}

void doSomething(void) throw();
void doSomething(void) throw()
{
    A obj;
    obj.i= 100;
    throw obj;
}


int _tmain(int argc, _TCHAR* argv[])
{
    set_unexpected (myunexpected);
    try 
    {
        doSomething();
    }
    catch (bad_exception be) 
    {
        puts("Caught something");
    }
    catch (A &obj) 
    {
        puts("Caught Integer");
    }
    return 0;
}
+1  A: 

Basically, exception specifications are almost useless and in many compilers implemented different to what the standard states. Look at your compiler documentation for more information.

http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx

I can imagine that this means in particular that the VS compiler will use the exception specification to avoid generating code required for stack unwinding and that in the event of an exception actually being thrown you will end up with undefined behavior.

David Rodríguez - dribeas
+6  A: 

Regarding exception specification, Visual Studio is not standard-conforming.

While the empty exception specification is somewhat useful (but, as said, not properly implemented by VS), in general exception specifications are seen as an experiment that failed.

sbi
@sbi Thanks! I was not aware of that but agreeably never have used exception specifications usually. The actual Q i had on my mind, doesnt hold good as well then.
Als