Skip to content

Commit c333647

Browse files
sternmullpcercuei
authored andcommitted
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 <[email protected]>
1 parent 82e9508 commit c333647

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

bindings/cpp/examples/iiopp-enum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ int main(int argc, char ** argv)
9393
{
9494
enumerateIioEntities();
9595
}
96-
catch (std::exception & e)
96+
catch (error & e)
9797
{
98-
cerr << "ERROR: " << e.what() << endl;
98+
cerr << "ERROR " << e.code().value() << ": " << e.what() << endl;
9999
return EXIT_FAILURE;
100100
}
101101

bindings/cpp/iiopp.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <string>
1919
#include <optional>
2020
#include <stdexcept>
21-
#include <sstream>
21+
#include <system_error>
2222
#include <cassert>
2323
#include <memory>
2424

@@ -70,6 +70,14 @@ class cstr
7070
operator char const * () const {return s;}
7171
};
7272

73+
/** @brief Thrown to report errors.
74+
*/
75+
class error : public std::system_error
76+
{
77+
public:
78+
using std::system_error::system_error;
79+
};
80+
7381
/** @brief Common interface for attribute access
7482
*/
7583
class IAttr
@@ -123,10 +131,7 @@ inline std::string err_str(int err)
123131
[[noreturn]] inline void err(int err, char const * ctx)
124132
{
125133
assert(err > 0);
126-
std::ostringstream s;
127-
s << ctx << ": " << err_str(err);
128-
s.flush();
129-
throw std::runtime_error(s.str());
134+
throw error(err, std::generic_category(), ctx);
130135
}
131136

132137
inline void check(int ret, char const * ctx)

0 commit comments

Comments
 (0)