right usage of std::uncaught_exception in a destructor

后端 未结 3 2009
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 05:06

There are some articles concluding \"never throw an exception from a destructor\", and \"std::uncaught_exception() is not useful\", for example:

  • http://www.got
3条回答
  •  囚心锁ツ
    2021-01-05 05:39

    Herb Sutter is referring to a different issue. He's talking about:

    try
    {
    }
    catch (...)
    {
        try
        {
            // here, std::uncaught_exception() will return true
            // but it is still safe to throw an exception because
            // we have opened a new try block
        }
        catch (...)
        {
        }
    }
    

    So the problem is that if std::uncaught_exception() returns true you don't know for sure whether you can safely throw an exception or not. You end up having to avoid throwing an exception when std::uncaught_exception() returns true just to be safe.

提交回复
热议问题