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?