C++ "terminate called without an active exception": causes and fixes
Codemia Knowledge Hub ·
"terminate called without an active exception" is a C++ runtime error: std::terminate() was called even though no exception is currently being thrown or propagated. It shows up in a few distinct situations, which makes it tricky to track down.
What std::terminate does
std::terminate() is part of the exception handling library and is called automatically when:
- an exception reaches the end of a
noexceptfunction, - an exception is not caught by any
catchclause, - another exception is thrown during stack unwinding while one is already active.
By default it calls std::abort(). You can install your own handler with std::set_terminate().
Situations that trigger the error
- A
std::threadis destroyed while still joinable. If a thread object goes out of scope beforejoin()ordetach()is called, its destructor callsstd::terminate(). This is the most common cause.
- Rethrowing when no exception is being handled. A bare
throw;rethrows the current exception. Outside acatchblock there is none, so the program terminates.
- Related: exceptions that escape. A destructor that throws during stack unwinding, or an exception that leaves a thread function, also calls
std::terminate(). Because an exception is active in those cases, libstdc++ prints "terminate called after throwing an instance of '...'" instead.
| Scenario | Description | Solution |
| Joinable thread destroyed | The std::thread destructor runs before join() or detach(). | Join or detach on every path, or use std::jthread (C++20). |
Bare throw; outside a handler | Rethrow with no exception being handled. | Use throw; only inside a catch block. |
| Exception escapes a destructor or thread | Prints "after throwing an instance of" instead. | Mark destructors noexcept; catch inside thread functions. |
Installing a custom terminate handler
A custom handler lets you log something before the program aborts:
How to prevent it
- Join or detach every thread. Call
join()ordetach()before astd::threadis destroyed, including on early-return and exception paths, or usestd::jthread, which joins in its destructor. - Do not throw from destructors. Mark destructors
noexceptwhere possible, and use another signaling mechanism if a destructor must report failure. - Rethrow carefully. Use
throw;only inside a catch block, and make sure the exception state is maintained. - Catch inside thread functions. Wrap the main logic of each thread in a try-catch block so its exceptions are handled without affecting the main thread.
Related reading
- C Thread Safe Integer
- C use of Eigen in tensorflow
- C using standard algorithms with strings, count_if with isdigit, function cast
- C Winsock non-blocking/async UDP socket
- CA2202, how to solve this case
- Calling remove in foreach loop in Java
- Calculate mean and standard deviation from a vector of samples in C using Boost
- Calculating factorial of large numbers in C
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.