Skip to content

Commit

Permalink
Add user_defined_exception example
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Oct 11, 2024
1 parent 300448d commit 25c165e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/xtd.core.examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@

## [Exceptions](exceptions/README.md)

* [exception_ptr](exception_ptr/README.md) shows how to use std::exception_ptr and [invalid_operation_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1invalid__operation__exception.html) exception.
* [argument_exception](exceptions/argument_exception/README.md) shows how to use [xtd::argument_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1argument__exception.html) exception.
* [exception_ptr](exceptions/exception_ptr/README.md) shows how to use std::exception_ptr and [invalid_operation_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1invalid__operation__exception.html) exception.
* [system_exception](exceptions/system_exception/README.md) shows how to use [xtd::system_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1system__exception.html) exception.
* [user_defined_exception](exceptions/user_defined_exception/README.md) shows how to use [xtd::exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html) exception.

## [Format](format/README.md)

Expand Down
1 change: 1 addition & 0 deletions examples/xtd.core.examples/exceptions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ add_projects(
argument_exception
exception_ptr
system_exception
user_defined_exception
)
1 change: 1 addition & 0 deletions examples/xtd.core.examples/exceptions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [argument_exception](argument_exception/README.md) shows how to use [xtd::argument_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1argument__exception.html) exception.
* [exception_ptr](exception_ptr/README.md) shows how to use std::exception_ptr and [invalid_operation_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1invalid__operation__exception.html) exception.
* [system_exception](system_exception/README.md) shows how to use [xtd::system_exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1system__exception.html) exception.
* [user_defined_exception](user_defined_exception/README.md) shows how to use [xtd::exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html) exception.

## Build and run any project

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(user_defined_exception)
find_package(xtd REQUIRED)
add_sources(README.md src/user_defined_exception.cpp)
target_type(CONSOLE_APPLICATION)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# user_defined_exception

Shows how to use [xtd::exception](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1exception.html) exception.

## Sources

[src/user_defined_exception.cpp](src/user_defined_exception.cpp)

[CMakeLists.txt](CMakeLists.txt)

## Build and run

Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:

```cmake
xtdc run
```

## Output

```
type = user_defined_exception::employee_list_not_found_exception
help_link =
h_result = 0x80131500 (2148734208)
inner_exception = (null)
message = The employee list does not exist.
source = user_defined_exception
stack_trace = at main [0x0000665C] in /Users/yves/Projects/xtd/examples/xtd.core.examples/exceptions/user_defined_exception/src/user_defined_exception.cpp:line 24
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <xtd/console>
#include <xtd/exception>

using namespace xtd;
using namespace xtd::diagnostics;

namespace user_defined_exception {
class employee_list_not_found_exception : public exception {
public:
employee_list_not_found_exception(const stack_frame& stack_frame = null) : exception {"The employee list does not exist."_t, stack_frame} {
}

employee_list_not_found_exception(const string& message, const stack_frame& stack_frame = null) : exception {message, stack_frame} {
}

template<typename exception_t>
employee_list_not_found_exception(const string& message, const exception_t& inner, const stack_frame& stack_frame = null) : exception {message, inner, stack_frame} {
}
};
}

auto main() -> int {
try {
throw user_defined_exception::employee_list_not_found_exception {csf_};
} catch(const exception& e) {
console::write_line("type = {}", e.get_type());
console::write_line("help_link = {}", e.help_link());
console::write_line("h_result = 0x{0:X8} ({0})", static_cast<uint>(e.h_result()));
console::write_line("inner_exception = {}", e.inner_exception());
console::write_line("message = {}", e.message());
console::write_line("source = {}", e.source());
console::write_line("stack_trace = {}", e.stack_trace());
}
}

// This code can produce the following output :
//
// type = user_defined_exception::employee_list_not_found_exception
// help_link =
// h_result = 0x80131500 (2148734208)
// inner_exception = (null)
// message = The employee list does not exist.
// source = user_defined_exception
// stack_trace = at main [0x0000665C] in /Users/yves/Projects/xtd/examples/xtd.core.examples/exceptions/user_defined_exception/src/user_defined_exception.cpp:line 24

0 comments on commit 25c165e

Please sign in to comment.