Should it be possible to use std::current_exception inside destructors of objects that are destroyed during stack unwinding?
Documentation on cppreference says:
If called during exception handling (typically, in a catch clause), captures the current exception object (...)
But it's not clear for me whether stack unwinding is a part of exception handling.
In some highest-ranked answer on stackoverflow author assumes that it's possible.
I did some test on my compiler (g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2) and it seems, that std::current_exception returns empty pointer in this case.
#include <exception> #include <stdexcept> #include <iostream> struct A { ~A() { std::clog << "in destructor"<<std::endl; std::clog << "uncaught_exception: " << std::uncaught_exception() << std::endl; std::clog << "current_exception: " << (bool)std::current_exception() << std::endl; } }; int main(int argc, char **) { try { A aa; std::clog << "before throw"<<std::endl; if(argc>1) throw std::runtime_error("oh no"); } catch(...) { std::clog << "in catch block"<<std::endl; std::clog << "uncaught_exception: " << std::uncaught_exception() << std::endl; std::clog << "current_exception: " << (bool)std::current_exception() << std::endl; } return 0; } The output is:
before throw in destructor uncaught_exception: 1 current_exception: 0 in catch block uncaught_exception: 0 current_exception: 1 Does anybody know what the standard says?