diff --git a/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions.md b/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions.md index a1907e800497..59635e86fdf7 100644 --- a/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions.md +++ b/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions.md @@ -1,6 +1,33 @@ -# How to create user-defined exceptions with localized exception messages (xtd.core) +# How to create user-defined exception (xtd.core) -Coming soon... +xtd provides a hierarchy of exception classes ultimately derived from the Exception base class. +However, if none of the predefined exceptions meet your needs, you can create your own exception class by deriving from the [xtd::exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html) class. + +When creating your own exceptions, end the class name of the user-defined exception with the word "_exception", and implement the three common constructors, as shown in the following example. +The example defines a new exception class named `employee_list_not_found_exception`. The class is derived from the [xtd::exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html) base class and includes three constructors. + +```cpp +#include + +using xtd; + +class employee_list_not_found_exception : public exception { +public: + employee_list_not_found_exception(const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::empty()) : exception {"The employee list does not exist."_t, stack_frame} { + } + + employee_list_not_found_exception(const string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::empty()) : exception {message, stack_frame} { + } + + template + employee_list_not_found_exception(const string& message, const exception_t& inner, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::empty()) : exception(message, inner, stack_frame) { + } +}; +``` + +> [!NOTE] +> In situations where you're using remoting, you must ensure that the metadata for any user-defined exceptions is available at the server (callee) and to the client (the proxy object or caller). +> For more information, see [Best practices for exceptions](/docs/documentation/Guides/xtd.core/Exceptions/exceptions_best_practices). # See also ​ diff --git a/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions_with_localized_exception_messages .md b/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions_with_localized_exception_messages .md new file mode 100644 index 000000000000..4ac85f4da3c6 --- /dev/null +++ b/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions_with_localized_exception_messages .md @@ -0,0 +1,39 @@ +# How to create user-defined exception with localized exception messages (xtd.core) + +xtd provides a hierarchy of exception classes ultimately derived from the Exception base class. +However, if none of the predefined exceptions meet your needs, you can create your own exception class by deriving from the [xtd::exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html) class. + +When creating your own exceptions, end the class name of the user-defined exception with the word "_exception", and implement the three common constructors, as shown in the following example. +The example defines a new exception class named `employee_list_not_found_exception`. The class is derived from the [xtd::exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html) base class and includes three constructors. + +```cpp +#include + +using xtd; + +class employee_list_not_found_exception : public exception { +public: + employee_list_not_found_exception(const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::empty()) : exception {"The employee list does not exist."_t, stack_frame} { + } + + employee_list_not_found_exception(const string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::empty()) : exception {message, stack_frame} { + } + + template + employee_list_not_found_exception(const string& message, const exception_t& inner, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::empty()) : exception(message, inner, stack_frame) { + } +}; +``` + +> [!NOTE] +> In situations where you're using remoting, you must ensure that the metadata for any user-defined exceptions is available at the server (callee) and to the client (the proxy object or caller). +> For more information, see [Best practices for exceptions](/docs/documentation/Guides/xtd.core/Exceptions/exceptions_best_practices). + +# See also +​ +* [Guides](/docs/documentation/Guides) +* [Documentation](/docs/documentation) + +© 2024 Gammasoft. + +[//]: # (https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-localized-exception-messages) diff --git a/docs/documentation/Guides/xtd.core/Exceptions/exception_class_and_properties.md b/docs/documentation/Guides/xtd.core/Exceptions/exception_class_and_properties.md index f4b1ea248cd9..47cd9e4e5852 100644 --- a/docs/documentation/Guides/xtd.core/Exceptions/exception_class_and_properties.md +++ b/docs/documentation/Guides/xtd.core/Exceptions/exception_class_and_properties.md @@ -24,6 +24,7 @@ We recommend that you throw and catch only objects that derive from [xtd::except # See also ​ +* [Exceptions](/docs/documentation/Guides/xtd.core/Exceptions/overview) * [Guides](/docs/documentation/Guides) * [Documentation](/docs/documentation) diff --git a/docs/documentation/Guides/xtd.core/Exceptions/exceptions_best_practices.md b/docs/documentation/Guides/xtd.core/Exceptions/exceptions_best_practices.md index ed5ad00f7099..af3c2794e866 100644 --- a/docs/documentation/Guides/xtd.core/Exceptions/exceptions_best_practices.md +++ b/docs/documentation/Guides/xtd.core/Exceptions/exceptions_best_practices.md @@ -320,6 +320,8 @@ Use at least the three common constructors when creating your own exception clas * [xtd::exception(const xtd::string &message, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html#ad43693d8a3723ec0b995123b2d14a297), which accepts a string message and optional stack frame. * [xtd::exception(const xtd::string &message, const std::exception &inner_exception, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html#a946c0dec93a72997929197b1699e7040), which accepts a string message, an inner exception and optional stack frame. +For an example, see [How to: Create user-defined exceptions](/docs/documentation/Guides/xtd.core/Exceptions/How-tos/create_user_defined_exceptions). + ### Provide additional properties as needed Provide additional properties for an exception (in addition to the custom message string) only when there's a programmatic scenario where the additional information is useful. @@ -327,6 +329,7 @@ For example, the [xtd::io::file_not_found_exception](https://gammasoft71.github. # See also ​ +* [Exceptions](/docs/documentation/Guides/xtd.core/Exceptions/overview) * [Guides](/docs/documentation/Guides) * [Documentation](/docs/documentation)