#include #include #include struct Worker { static int alive; Worker() { ++alive; } ~Worker() { --alive; } }; int Worker::alive = 0; void run(bool fail) { auto worker = std::make_unique(); if (fail) throw std::runtime_error("cancelled"); } int main() { bool caught = false; try { run(true); } catch (const std::runtime_error&) { caught = true; } assert(caught && Worker::alive == 0); run(false); assert(Worker::alive == 0); }