#include "ErrorContext.h" #includevoid foo() { DESC() << "Error occurred in foo()"; // Some code DESC() << "Another error occurred in foo()"; } int main() { try { foo(); } catch (std::runtime_error& err) { std::cerr << err.what() << std::endl; } return 0; }
#include "ErrorContext.h" #includeIn this example, the DESC() macro is used to create a new error context with a specific name. The catch block in the 'foo()' function catches the exception thrown by the 'bar()' function and logs the error message in a context specific to the 'bar()' function. The ErrorContext DESC library can be found in the Boost C++ library.void bar() { DESC() << "Error occurred in bar()"; throw std::runtime_error("Error in bar()"); } void foo() { DESC() << "Starting foo()"; try { bar(); } catch (std::runtime_error& err) { DESC("bar()") << err.what(); throw; } } int main() { try { foo(); } catch (std::runtime_error& err) { std::cerr << err.what() << std::endl; } return 0; }