Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 418
Ralph Tandetzky Avatar asked Oct 20 '22 23:10

Ralph Tandetzky


1 Answers

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.

like image 65
sasha.sochka Avatar answered Oct 24 '22 05:10

sasha.sochka