How to use errno in C++?
Last Updated :
30 Jan, 2024
In C++ errno
is a preprocessor macro that is used for error indication and reporting errors that occur during a function call. It contains error codes so if a call to a function fails somehow then the errno is set to a value that corresponds to the error.
errno is defined in the header file <cerrno>
i
n C++.
Working of errno
in C++
The errno works as the explain below:
Initially at the beginning of the program, the errno
is set to zero and it changes when any error occurs.
- If a function call fails it sets
errno
, then we check its value. There is no change in the value errno
if no errors occur.
Examples of errorno in C++
The following examples illustrate how we can use errno in different scenarios.
Example 1
The below example demonstrates the usage of errno for reading a file because when we try to open a file it may throw an error.
C++
#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
const char * filename = "myfile.txt" ;
ifstream file(filename);
if (!file) {
switch ( errno ) {
case ENOENT:
cerr << "Error: File '" << filename
<< "' doesn't exist." << endl;
break ;
case EACCES:
cerr << "Error: Permission denied for file '"
<< filename << "'." << endl;
break ;
default :
cerr << "Error opening file '" << filename
<< "': " << strerror ( errno ) << endl;
break ;
}
return 1;
}
file.close();
return 0;
}
|
Output
Error: File 'myfile.txt' doesn't exist.
Explanation: The above example shows how to handle error that we get when we try to open a file using ifstream
. It checks for conditions that may give error like file not existing or access being denied by using the errno
variable.
Note: Always clear errno after handling errors and use errno=0 to reset the value after handling an error.
Example 2
The below example demonstrates the use of errno to detect error in mathematical operations.
C++
#include <cerrno>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double x = -1.0;
double result = sqrt (x);
if ( errno == EDOM) {
cerr << "Error: Cannot take the square root "
"of a negative number."
<< endl;
return 1;
}
cout << "Square root of " << x << " is: " << result
<< endl;
return 0;
}
|
Output
Error: Cannot take the square root of a negative number.
Explanation: The above example checks for EDOM after taking the mathematical values Error. When we try to calculate the square root of a negative number like (-2
), that are actually not defined and leads to a domain error.
Error Codes Associated with errorno in C++
There are lots of codes associated with errorno in C++ but some of the commonly used error codes are listed in the below table:
1 |
EPERM |
Operation not permitted |
2 |
ENOENT |
No such file or directory |
3 |
ESRCH |
No such process |
5 |
EIO |
I/O error |
7 |
E2BIG |
Argument list too long |
9 |
EBADF |
Bad file number |
10 |
ECHILD |
No child processes |
11 |
EAGAIN |
Try again |
12 |
ENOMEM |
Out of memory |
13 |
EACCES |
Permission denied |
Share your thoughts in the comments
Please Login to comment...