Capturing and Rethrowing Custom Exceptions

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

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.

Storing and Rethrowing Exceptions

This lesson offers a comprehensive guide to storing and rethrowing exceptions

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Capturing Exceptions in a Different Thread
How can I capture an exception thrown in one thread and handle it in a different thread?
Handling Nested Exceptions
What is a good strategy for handling exceptions that are thrown while already handling another exception?
Rethrowing Exceptions and noexcept
What happens if I rethrow an exception in a noexcept function?
Using std::exception_ptr Across DLL Boundaries
Can I use std::exception_ptr to transfer exceptions across DLL boundaries?
std::exception_ptr and Memory Management
Are there any memory management considerations when using std::exception_ptr?
Performance Considerations with std::exception_ptr
What are the performance implications of using std::exception_ptr?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant