Currently in C++ you can't attach any additional information (like stacktrace, etc) to an exception thrown in third-party libraries. How C++ language design could look like in this regard?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The inability to attach additional context (like stack traces, error codes, or custom metadata) to exceptions thrown by third-party libraries is a well-known limitation in C++. Addressing this issue would require changes to the language and standard library design. Here’s how C++ could evolve to support richer exception information:
1. Enhance std::exception Hierarchy
Rich Exception Types: Introduce new standard exception classes or mechanisms that allow attaching additional metadata. For example:
Dynamic Metadata: Allow exceptions to carry dynamic data, such as key-value pairs or structured information, without requiring a new exception type for each variation.
2. Standardize Stack Trace Capture
Built-in Stack Trace Support: Provide a standard way to capture and store stack traces when an exception is thrown. This could be part of the exception object itself or accessible via a standard interface.
cpp
try {
// Third-party code that throws
} catch (const std::exception& e) {
std::string trace = e.stacktrace();
// Use trace for debugging
}
Compiler and Runtime Support: Require compilers and runtime systems to capture stack traces automatically when exceptions are thrown, making this information available to catch blocks.
3. Extend std::exception_ptr for Metadata
Metadata in std::exception_ptr: Enhance std::exception_ptr to carry additional metadata alongside the exception object. This could be done by allowing exception_ptr to encapsulate a richer data structure.
cpp
std::exception_ptr ptr = std::current_exception();
if (ptr) {
auto metadata = std::get_exception_metadata(ptr);
// Access metadata like stacktrace, error codes, etc.
}
4. Introduce a Context-Aware Exception Mechanism
Context Wrappers: Provide a way to wrap exceptions with additional context as they propagate up the call stack.
Automatic Context Attachment: Allow compilers or libraries to automatically attach context (e.g., function names, line numbers) as exceptions propagate.
Integrate std::source_location: Use the existing std::source_location (introduced in C++20) to automatically capture the location where the exception was thrown.
cpp
try {
// Third-party code that throws
} catch (const std::exception& e) {
std::source_location loc = e.location();
// Use loc for debugging
}
6. Allow Custom Exception Wrappers
Exception Decorators: Enable developers to decorate exceptions with additional information without modifying the original exception type.
To address this limitation, C++ could introduce richer exception types, standardize stack trace capture, and provide mechanisms for attaching metadata to exceptions. These changes would require collaboration between the C++ standards committee, compiler developers, and library authors to ensure backward compatibility and widespread adoption.