I’ve noticed that there are a few more interesting declarations in <exception> in C++11. Can anybody shed any light on what they mean and how to use them?
The ones I’m wondering about are:
::std::nested_exception::std::throw_with_nested::std::rethrow_if_nested
Additionally, while they seem self-explanatory, it might be nice to know how these worked:
::std::exception_ptr::std::make_exception_ptr::std::current_exception::std::rethrow_exception
Some high level code will generically just catch
std::exceptionand print thewhat(). You want to squeeze as much information as possible to this generic mechanism, yet without losing any information. Consider an implementation of some archive library:The information available to the archive is not recorded (e.g. filename). Besides you would like to distinguish exceptions that came from the archive class from other exceptions.
Now we added higher-level semantic information that
archiveclass knows, but we also lost the information about the original cause of problem (the type ofe).nested_exceptionis meant to solve this problem:All the available information is recorded. We can now generically retrieve it in the catch site:
The output will be more descriptive than before. It will describe the problem starting from the high-level to the low-level:
Or perhaps:
The functions working with
exception_ptrare designed to transfer exceptions between threads, or more generally, store an exception for later use. How they work depends on the implementation. The intention was thatexception_ptrwill be a shared pointer to the exception object. However when this pointer is created, when throwing the exception or when trying to get anexception_ptrto it, is subject to the implementation. The implementation is still free to copy the exception when you callcurrent_exception().