c++c++11error-handlingstd-system-error

How do I use `std::error_category` and the other stuff that is in the system_error header?


There are enough error handling strategies in C++ already. We have exception handling, error return codes and this ERRNO mess. What role does the system_error header play here? How do I use the features in there? To me, it just looks randomly put together. I'm using the cppreference site as a reference.


Solution

  • You can throw and catch it as a normal exception. It's just a part of std::exception hierarchy. std::system_error extends std::runtime_error which extends std::exception

    When should it be used? Typically it's used for converting C-style ERRNO errors to throw&catch handling with encapsulated error-code inside the object. This is heavily used by standard library itself especially in the new libraries working with OS-specific stuff, e.g. in <thread> library.