Handling Nested Exceptions

What is a good strategy for handling exceptions that are thrown while already handling another exception?

When an exception is thrown while already handling another exception, it's called a nested exception. This can lead to tricky situations and potential resource leaks if not handled carefully.

One strategy is to catch and handle the nested exception separately, and then continue handling the original exception:

#include <iostream>
#include <stdexcept>

void handleException() {
  try {
    throw std::runtime_error(
      "Original exception");
  } catch (const std::exception& e) {
    std::cout << "Handling: "
      << e.what() << "\n";
    try {
      throw std::logic_error("Nested exception");
    } catch (const std::exception& nested_e) {
      std::cout << "Handling nested: "
        << nested_e.what() << "\n";
    }
    std::cout << "Continuing handling "
      "original exception\n";
  }
}

int main() {
  handleException();
}
Handling: Original exception
Handling nested: Nested exception
Continuing handling original exception

Another approach is to capture the nested exception using std::current_exception(), store it in a std::exception_ptr, and then rethrow it after finishing handling the original exception:

#include <exception>
#include <iostream>
#include <stdexcept>

void handleException() {
  std::exception_ptr nested_eptr;
  try {
    throw std::runtime_error(
      "Original exception");
  } catch (const std::exception& e) {
    std::cout << "Handling: " << e.what() << "\n";
    try {
      throw std::logic_error("Nested exception");
    } catch (...) {
      nested_eptr = std::current_exception();
    }
    std::cout << "Continuing handling "
      "original exception\n";
  }
  if (nested_eptr) {
    std::rethrow_exception(nested_eptr);
  }
}

int main() {
  try {
    handleException();
  } catch (const std::exception& e) {
    std::cout << "Caught rethrown nested "
      "exception: " << e.what() << "\n";
  }
}
Handling: Original exception
Continuing handling original exception
Caught rethrown nested exception: Nested exception

This ensures the nested exception isn't lost and can be handled at a higher level if needed.

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?
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?
Capturing and Rethrowing Custom Exceptions
Can I use std::exception_ptr to capture and rethrow custom exceptions?
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