C++
terminate
exception handling
error
debugging

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 noexcept function,
  • an exception is not caught by any catch clause,
  • 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

  1. A std::thread is destroyed while still joinable. If a thread object goes out of scope before join() or detach() is called, its destructor calls std::terminate(). This is the most common cause.
cpp
1      #include <thread>
2
3      void work() {}
4
5      int main() {
6          std::thread t(work);
7      }   // t is still joinable when its destructor runs
  1. Rethrowing when no exception is being handled. A bare throw; rethrows the current exception. Outside a catch block there is none, so the program terminates.
cpp
1      void rethrow() {
2          throw;  // Re-throwing without a current exception
3      }
4
5      int main() {
6          rethrow();
7      }
  1. 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.
cpp
1      #include <stdexcept>
2      #include <thread>
3
4      void threadFunction() {
5          throw std::runtime_error("Error in thread");
6      }
7
8      int main() {
9          std::thread t(threadFunction);
10          t.join();
11      }
ScenarioDescriptionSolution
Joinable thread destroyedThe std::thread destructor runs before join() or detach().Join or detach on every path, or use std::jthread (C++20).
Bare throw; outside a handlerRethrow with no exception being handled.Use throw; only inside a catch block.
Exception escapes a destructor or threadPrints "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:

cpp
1#include <iostream>
2#include <exception>
3#include <cstdlib>
4
5void customTerminate() {
6    std::cerr << "Custom terminate handler: Termination!\n";
7    std::abort();
8}
9
10int main() {
11    std::set_terminate(customTerminate);
12
13    try {
14        throw std::runtime_error("This exception causes terminate call.");
15    } catch (const std::runtime_error& e) {
16        std::cout << "Caught exception: " << e.what() << "\n";
17    }
18
19    throw std::exception();  // Uncaught leads to std::terminate()
20}

How to prevent it

  • Join or detach every thread. Call join() or detach() before a std::thread is destroyed, including on early-return and exception paths, or use std::jthread, which joins in its destructor.
  • Do not throw from destructors. Mark destructors noexcept where 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms