chsvlib
chsv helper source code
chsvexception\nested_exceptions.cpp

An example of using the Chusov::Exceptions::throw_with_nested function and the Chusov::Exceptions::rethrow_if_nested function.

#include <iostream>
#include "chsvexception.h"
void f3()
{
}
void f2()
{
try
{
f3();
{
Chusov::Exceptions::throw_with_nested(std::system_error(ex.Code(), std::generic_category()));
}
}
struct Obj
{
void fo()
{
try
{
f2();
}catch(...)
{
}
}
};
void f1()
{
try
{
Obj x;
x.fo();
}catch (...)
{
}
}
#include <codecvt>
#include <locale>
#include <string>
template <class E>
void PrintNestedExceptions(E&& e, unsigned level = 1)
{
try
{
{
std::wcout << std::wstring(level, L' ') << ex.What() << std::endl;
PrintNestedExceptions(ex, ++level);
}catch(std::exception& ex)
{
std::wcout << std::wstring(level, L' ') << std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(ex.what()) << std::endl;
PrintNestedExceptions(ex, ++level);
}
}
int wmain(int argc, wchar_t **argv)
{
try
{
f1();
{
std::wcout << L"Exception caught: " << ex.What() << std::endl;
std::wcout << L"Nested exceptions (from top to bottom):\n";
PrintNestedExceptions(ex);
}
return 0;
}
/*
Output:
Exception caught: Invalid parameter
Nested exceptions (from top to bottom):
Access is denied: f2 failed
unknown error
Insufficient buffer
*/
The file contains definitions of various exceptions thrown by the mechanisms of the chsvcode library ...
Defines a class of objects specifying various exceptions with extended support for description format...
Definition: chsvexception.h:392
virtual const wchar_t * What() const noexcept
Returns a description, corresponding to the exception, represented as a wide string.
Definition: chsvexception.h:573
A class template specifying a generic exception which has a corresponding integral error code.
Definition: chsvexception.h:588
code_t Code() const noexcept
Returns an error code, the exception object is bound with.
Definition: chsvexception.h:1395
void ChsvExceptionByPosixCode(errno_t nCode, const char *restrict pszDescription, DescriptionFormatParams ... format_params)
Creates an instance of the ChsvCodeExceptionTempl class and throws it as an exception....
Definition: chsvexception.h:4259
void rethrow_if_nested(E &&e)
Implements a behaviour of the standard std::rethrow_if_nested function checking if the most derived d...
Definition: chsvexception.h:5398
void throw_with_nested(E &&e)
Implements a behaviour of the standard std::throw_with_nested function storing an exception having be...
Definition: chsvexception.h:5375
std::basic_string< wchar_t, std::char_traits< wchar_t >, ::Chusov::Memory::allocator< wchar_t > > wstring
An instantiation of the standard basic_string class template for handling wide strings allocated and ...
Definition: chsvstringex.h:133
Access denied exception.
Definition: chsvexception.h:2412
Invalid parameter exception.
Definition: chsvexception.h:1741