std::terminate and the noexcept specifier

Logging with a custom terminate handler

How can I use a custom terminate handler to log information about an unhandled exception?

Abstract art representing computer programming

You can use a custom terminate handler to log information about an unhandled exception before terminating the program. Here's an example of how you can set up a custom terminate handler for logging:

#include <exception>
#include <iostream>

void customTerminateHandler() {
  try {
    std::exception_ptr exPtr =
      std::current_exception();
    if (exPtr) {
      std::rethrow_exception(exPtr);
    }
  } catch (const std::exception& ex) {
    std::cout << "Unhandled exception: "
      << ex.what() << '\n';  
  } catch (...) {
    std::cout << "Unhandled unknown exception\n";
  }
  std::abort();
}

int main() {
  std::set_terminate(customTerminateHandler);

  try {
    throw std::runtime_error(
      "Something went wrong");
  } catch (const std::logic_error& ex) {
    // Catch and handle logic_error exceptions
  }
}
Unhandled exception: Something went wrong

In this example:

  1. We define a custom terminate handler function customTerminateHandler.
  2. Inside the handler, we retrieve the currently thrown exception using std::current_exception().
  3. If an exception pointer is obtained, we rethrow the exception to catch and log its details.
  4. We catch specific exception types (e.g., std::exception) and log their error messages using ex.what().
  5. For unknown exceptions, we log a generic message.
  6. Finally, we call std::abort() to terminate the program.

In the main() function:

  1. We set the custom terminate handler using std::set_terminate().
  2. We intentionally throw an exception that is not caught by the provided catch block.

When the unhandled exception is thrown, the custom terminate handler is invoked, logging the exception details before terminating the program.

This Question is from the Lesson:

std::terminate and the noexcept specifier

This lesson explores the std::terminate function and noexcept specifier, with particular focus on their interactions with move semantics.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

std::terminate and the noexcept specifier

This lesson explores the std::terminate function and noexcept specifier, with particular focus on their interactions with move semantics.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved