chsvlib
chsv helper source code

◆ ExceptionWithGenericCode() [9/10]

ExceptionWithGenericCode ( code_t  nCode,
const std::basic_string< char_t, traits_t, alloc_t > &  strDescriptionFormat 
)
noexcept

Constructs an exception object by a specified error code, and a description provided by an std::basic_string object. .

Template Parameters
char_tis a type of a symbol of an std::basic_string object specifying the description.
traits_tis a type of a character traits class that is used by the std::basic_string template.
alloc_tis a type of an allocator class used by the std::basic_string template.
Parameters
[in]nCodeis a code to be associated with the constructed exception object.
[in]strDescriptionFormatis an std::basic_string object specifying the description. Unlike other constructors, the current one performs neither tag nor Printf formatting. The string is set as the description of the exception "as-is". However, if the string is empty, the actual description set is a notification saying that the description is not specified.

The constructor creates an instance of the ExceptionWithGenericCode<code_t> class with associated error code and the description. The description is constructed without any formatting, performed by other constructors of the ExceptionWithGenericCode class. For example, the following code

throw ExceptionWithGenericCode<errno_t>(EINVAL,
std::wstring(L"Error <").append(std::to_wstring(EINVAL)).append(L">: ").append(std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(std::strerror(EINVAL))));
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

results in the exception description (with MSVC defined errno subsystem) to be :

Error <22>: Invalid argument

The same, except for a base used to represent the error code value, result can be achieved by using the another overload of ExceptionWithGenericCode, as follows:

throw ExceptionWithGenericCode<errno_t>(EINVAL, &std::strerror, std::wstring(L"Error <<<CODE>>>: <DESCRIPTION>"));

or, using overload with tag formatting and Printf formatting, as follows:

throw ExceptionWithGenericCode<errno_t>(EINVAL, L"Error <<<CODE>>>: %s", strerror(EINVAL));

{.cpp}

Remarks
The function is provided only for compilers with expression SFINAE support. For older compilers use an overload which takes nullptr in the place of the omitted functor.
See also
ExceptionWithGenericCode(code_t nCode, fn_t&& GetDescription, const char_t* restrict lpszDescriptionFormat, DescriptionFormatParams... formatting_params);
ExceptionWithGenericCode(code_t nCode, fn_t&& GetDescription, Exception::vararg_tag_t, _In_opt_z_ const char_t* RESTRICT lpszDescriptionFormat, std::va_list ap);
ExceptionWithGenericCode(code_t nCode, Exception::vararg_tag_t, _In_opt_z_ const char_t* RESTRICT lpszDescriptionFormat, std::va_list ap).