我正在研究 C++11 标准。我想了解 error_code 和 errno 是否相互关联?如果是那么如何? 如果否,那么我应该在哪些条件下设置 errno 以及在哪些条件下设置 error_code?p>
我做了一个小测试程序来理解这一点,但仍然有点困惑。请帮忙。
#include <iostream>
#include <system_error>
#include <thread>
#include <cstring>
#include <cerrno>
#include <cstdio>
using namespace std;
int main()
{
try
{
thread().detach();
} catch (const system_error & e) {
cout<<"Error code value - "<<e.code().value()<<" ; Meaning - "<<e.what()<<endl;
cout<<"Error no. - "<<errno<<" ; Meaning - "<<strerror(errno)<<endl;
}
}
Output -
Error code value - 22 ; Meaning - Invalid argument
Error no. - 0 ; Meaning - Success
最佳答案
errno
由那些将其记录为遇到错误的副作用的函数使用 - 这些函数是 C 库或从不抛出异常的操作系统函数。 system_error
由 C++ 标准库使用,用于在您使用已记录的库设施抛出该异常时使用。完全分开。最后,请阅读您的文档!
关于c++ - error_code 与 errno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15826933/