Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error_code vs errno

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
like image 776
tshah06 Avatar asked Apr 05 '13 05:04

tshah06


1 Answers

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!

like image 162
Tony Delroy Avatar answered Oct 29 '22 02:10

Tony Delroy