Files
openmethod/doc/error_handling.adoc
Jean-Louis Leroy 5e0fa8ee4b inception
2025-03-08 15:31:25 -05:00

56 lines
1.3 KiB
Plaintext

## Error Handling
When an error is encountered, the program is terminated by a call to `abort`. If
the policy contains an `error_handler` facet, it provides an `error` member
function (or overloaded functions) to be called with an object identifying the
error. The `release` and `debug` policies implement the error facet with
`vectored_error_handler`, which wraps the error object in a variant, and calls a
handler via a `std::function`. By default, it prints a description of the error
to `stderr` in the `debug` policy, and does nothing in the `release` policy. The
handler can be set with `set_error_handler`:
[source,c++]
----
include::{examplesdir}/vectored_error_handler.cpp[tag=example]
----
Output:
[source,console]
----
spin
not implemented
spin
----
We can also replace the `error_handler` facet with our own. For example:
[source,c++]
----
include::{examplesdir}/throw_error_handler.cpp[tag=example]
----
[source,console]
----
spin
not implemented
spin
----
Stock facet `throw_error_handler` does this for all the exception types:
```c++
namespace boost::openmethod::policies {
struct throw_error_handler : error_handler {
template<class Error>
[[noreturn]] static auto error(const Error& error) -> void {
throw error;
}
};
} // namespace boost::openmethod::policies
```