I am studying C++11 standards. I wanted to understand if error_code and errno related to each other? If yes then how? If no then in which conditions i should expect errno to be set and in which conditions error_code will be set?
I did a small test program to understand this but still little confused. Please help.
#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
is used by the those functions that document that as a side effect of their encountering an error - those functions are C library or OS functions that never throw exceptions. system_error
is a used by the C++ Standard Library for when you're using library facilities documented to throw that exception. Completely separate. Ultimately, read your docs!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With