Skip to content

Commit

Permalink
iiopp: exceptions now have a specific type, derived from std::system_…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
sternmull authored and pcercuei committed May 24, 2023
1 parent 82e9508 commit c333647
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bindings/cpp/examples/iiopp-enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 10 additions & 5 deletions bindings/cpp/iiopp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <string>
#include <optional>
#include <stdexcept>
#include <sstream>
#include <system_error>
#include <cassert>
#include <memory>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c333647

Please sign in to comment.