Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Oct 4, 2024
1 parent 97658bd commit bd5ddab
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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 <xtd/exception>

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<typename exception_t>
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <xtd/exception>

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<typename exception_t>
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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,16 @@ 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.
For example, the [xtd::io::file_not_found_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1io_1_1file__not__found__exception.html) provides the [file_name](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1io_1_1file__not__found__exception.html#a7cd16d6b427a9f14245ad77bcc19b4eb) property.
# See also
* [Exceptions](/docs/documentation/Guides/xtd.core/Exceptions/overview)
* [Guides](/docs/documentation/Guides)
* [Documentation](/docs/documentation)
Expand Down

0 comments on commit bd5ddab

Please sign in to comment.