【发布时间】:2017-09-22 17:55:29
【问题描述】:
我有这个抛出异常的代码(非常类似于what is suggested here):
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;
}
}
以及用于测试特定异常原因的代码:
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;
}
}
这似乎有点不必要的冗长,也不是正确的做事方式。有一般错误的整个想法和一个完整的枚举(参见::std::errc)以及某种应该在系统特定错误代码和这些一般错误之间转换的系统。
我想使用通用错误代码和类别,但我似乎无法让它们工作。我该怎么做?
【问题讨论】:
标签: c++ c++11 exception-handling