Omnifarious
Omnifarious

Reputation: 56048

How do I use the generic error codes enum with system_category error codes from <system_error>?

I have this code (very similar to what is suggested here) that throws an exception:

int accept4(int sockfd, sockaddr *addr, socklen_t *addrlen, int flags)
{
   const int fd = ::accept4(sockfd, addr, addrlen, flags);
   if (fd < 0)
   {
       const auto tmp = errno;
       throw ::std::system_error(tmp, ::std::system_category(), "accept4(2)");
   }
   else
   {
      return fd;
   }
}

And this code for testing for a particular exception reason:

catch (const ::std::system_error &e)
{
   static const auto block_err = ::std::system_error(EWOULDBLOCK,
                                                     ::std::system_category());

   const auto ecode = e.code(); 

   // EWOULBLOCK is fine, everything else, re-throw it.
   if (block_err.code() != ecode)
   {
      throw;
   }
}

This seems kind of needlessly verbose and not quite the right way to do things. There's this whole idea of generic errors and a whole enum (see ::std::errc) full of them along with some kind of system that's supposed to convert between system specific error codes and these generic errors.

I want to use the generic error codes and categories, and I can't seem to get them to work. How do I make that work?

Upvotes: 1

Views: 630

Answers (1)

T.C.
T.C.

Reputation: 137315

On an implementation of sufficiently high quality*, it suffices to do

if (e.code() != std::errc::operation_would_block)
    throw;

If not, you are stuck with

if (e.code() != std::error_code(EWOULDBLOCK, std::system_category()))
    throw;

There's certainly no need to construct another system_error just for the error code inside.


* It needs to implement system_category()'s default_error_condition to appropriately map the error to generic_category().

Upvotes: 1

Related Questions