From c3336470788ed31670b578237fd49f2923c15df6 Mon Sep 17 00:00:00 2001 From: Tilman Blumhagen Date: Fri, 19 May 2023 14:47:58 +0200 Subject: [PATCH] iiopp: exceptions now have a specific type, derived from std::system_error std::system_error is better at reporting errno-based errors: It also reports the error code while runtime_error only reported a message. Also throwing a specific iiopp::error class allows applications to discriminate them from other kinds of errors. Signed-off-by: Tilman Blumhagen --- bindings/cpp/examples/iiopp-enum.cpp | 4 ++-- bindings/cpp/iiopp.h | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/bindings/cpp/examples/iiopp-enum.cpp b/bindings/cpp/examples/iiopp-enum.cpp index 5f4388305..19eb46eb5 100644 --- a/bindings/cpp/examples/iiopp-enum.cpp +++ b/bindings/cpp/examples/iiopp-enum.cpp @@ -93,9 +93,9 @@ int main(int argc, char ** argv) { enumerateIioEntities(); } - catch (std::exception & e) + catch (error & e) { - cerr << "ERROR: " << e.what() << endl; + cerr << "ERROR " << e.code().value() << ": " << e.what() << endl; return EXIT_FAILURE; } diff --git a/bindings/cpp/iiopp.h b/bindings/cpp/iiopp.h index 6a5cc3ffb..983d2ecbe 100644 --- a/bindings/cpp/iiopp.h +++ b/bindings/cpp/iiopp.h @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include @@ -70,6 +70,14 @@ class cstr operator char const * () const {return s;} }; +/** @brief Thrown to report errors. + */ +class error : public std::system_error +{ +public: + using std::system_error::system_error; +}; + /** @brief Common interface for attribute access */ class IAttr @@ -123,10 +131,7 @@ inline std::string err_str(int err) [[noreturn]] inline void err(int err, char const * ctx) { assert(err > 0); - std::ostringstream s; - s << ctx << ": " << err_str(err); - s.flush(); - throw std::runtime_error(s.str()); + throw error(err, std::generic_category(), ctx); } inline void check(int ret, char const * ctx)