Storing and Rethrowing Exceptions

Capturing and Rethrowing Custom Exceptions

Can I use std::exception_ptr to capture and rethrow custom exceptions?

Abstract art representing computer programming

Yes, std::exception_ptr can be used to capture and rethrow any type of exception, including custom exception types.

Here's an example that demonstrates this:

#include <exception>
#include <iostream>
#include <string>

class CustomException : public std::exception {
public:
  explicit CustomException(
    const std::string& what) : m_what(what) {}
  const char* what() const noexcept override {
    return m_what.c_str();
  }
private:
  std::string m_what;
};

void throwCustomException() {
  throw CustomException(
    "This is a custom exception");
}

std::exception_ptr captureException() {
  try {
    throwCustomException();
  } catch (...) {
    return std::current_exception();
  }
  return nullptr;
}

void rethrowException(std::exception_ptr eptr) {
  try {
    if (eptr) {
      std::rethrow_exception(eptr);
    }
  } catch (const CustomException& e) {
    std::cout << "Caught CustomException: "
      << e.what() << "\n";
  } catch (const std::exception& e) {
    std::cout << "Caught std::exception: "
      << e.what() << "\n";
  }
}

int main() {
  std::exception_ptr eptr = captureException();
  rethrowException(eptr);
}
Caught CustomException: This is a custom exception

In this example:

  1. We define a custom exception class CustomException that derives from std::exception.
  2. The function throwCustomException() throws a CustomException.
  3. The function captureException() calls throwCustomException(), catches the exception, and captures it into a std::exception_ptr using std::current_exception().
  4. The main() function calls captureException() to get the std::exception_ptr, and then passes it to rethrowException().
  5. rethrowException() rethrows the exception using std::rethrow_exception(). It then catches the rethrown exception, first trying to catch CustomException, and then std::exception.

This demonstrates that std::exception_ptr can indeed capture and rethrow custom exception types. The custom exception can be caught and handled as expected when it's rethrown.

This capability is particularly useful when you need to transport exceptions through interfaces that don't know about your custom exception types, such as between layers of a complex system or across DLL boundaries.

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

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:

  • 125 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