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

主宰稳场 提交于 2019-12-11 05:46:51

问题


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?


回答1:


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().



来源:https://stackoverflow.com/questions/46370615/how-do-i-use-the-generic-error-codes-enum-with-system-category-error-codes-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!