Why do I always get “terminate called after throwing an instance of…” when throwing in my destructor?
问题 I'm trying to write a unit test that detects an invalid use of the lock() feature of my class. In order to do so, I want to use the destructor and throw an exception from there. Unfortunately, instead of catching the exception, g++ decides to call std::terminate(). There is a very simplified version of the class: class A { public: A() : f_lock(0) {} ~A() { if(f_lock) throw my_exception("still locked"); } lock() { ++f_lock; } unlock() { --f_lock; } private: int f_lock; }; There is a valid test