Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the try / catch in initialization list works?

Tags:

We consider that an exception in initialization may happen. So we write try / catch block.

int f(){
    throw 1;
}

class A
{
public:
    A() try : _k(f())
    {}
    catch (int)
    {
        std::cout << "Exception 1" << std::endl;
    }

private:
    int _k;
};

But the catch rethrows exception on one level deeper. Thats means that next code

try
{
    A a;
} catch(int)
{
    std::cout << "Exception 2" << std::endl;
}

will output:

Exception 1
Exception 2

Why this try / catch block behaves not the same way as ordinary try / catch block?

Full code example: http://ideone.com/XjY2d