Unit IV - Error Handling
Unit IV - Error Handling
Error Handling in C
Last Updated : 07 Aug, 2023
Although C does not provide direct support to error handling (or exception
handling), there are ways through which error handling can be done in C.
A programmer has to prevent errors in the first place and test return
values from the functions.
What is errno?
errno is a global variable indicating the error occurred during any function
call and it is defined inside <errno.h> header file.
Open In App
1 Operation not permitted
3 No such process
5 I/O error
10 No child processes
11 Try again
12 Out of memory
13 Permission denied
Example of errno
C Open In App
#include <errno.h>
#include <stdio.h>
int main()
{
// If a file is opened which does not exist,
// then it will be an error and corresponding
// errno value will be set
FILE* fp;
return 0;
}
Output
Value of errno: 2
Note: Here the errno is set to 2 which means “No such file or directory”. It
may give Errno 13 in online IDE, which says permission denied.
1. perror()
2. strerr()
3. ferror()
4. feof()
5. clearerr()
6. Exit Status
7. Divide by Zero Error
Syntax
Parameters
Example
int main()
{
FILE* fp;
return 0;
}
Open In App
Output
Value of errno: 2
Message from perror: No such file or directory
2. strerror()
The strerror() function is also used to show the error description. This
function returns a pointer to the textual representation of the current errno
value.
Syntax
Parameters
Example
int main()
{
FILE* fp;
return 0;
}
Output
Value of errno: 2
The error message is : No such file or directory
3. ferror()
Syntax
Parameters
stream: It is the pointer that points to the FILE for which we want to
check the error.
Return Value
Example
int main()
{
// Open the file in read mode
FILE* file = fopen("nonexistent_file.txt",
Open In App "r");
if (file == NULL) {
// Print an error message
// if file opening fails
perror("Error opening file");
// Return with non-zero exit status to
// indicate an error
return 1;
}
int c;
// Process the character
// Add your code here to perform operations on each
// character read from the file
while ((c = fgetc(file)) != EOF) {
}
if (ferror(file)) {
// Print an error message if an error occurred
// during file reading
printf(
"An error occurred while reading the file.\n");
}
else {
// Print success message if file reading completed
// without errors
printf("File read successfully.\n");
}
// Close the file
fclose(file);
// Return with zero exit status to indicate successful
// execution
return 0;
}
Output
4. feof()
Parameters
stream: It is the pointer that points to the FILE for which we want to
check the error.
Return Value
Example
int main()
{
FILE* fptr = fopen("filename.txt", "r");
if (fptr == NULL) {
// Print error message if file opening fails
perror("Error opening the file");
return 1;
}
if (ferror(fptr)) {
// Print error message if file operation fails
perror("Error occurred during file operations");
// Handle the error
}
// Close the file
fclose(fptr);
return 0;
}
Open In App
Output
5. clearerr()
Syntax
Parameters
stream: It is the pointer that points to the FILE for which we want to
check the error.
Example
#include <stdio.h>
int main()
{
FILE* file = fopen("file.txt", "r");
// Open the file in read mode
if (file == NULL) {
// Print an error message
// if file opening fails
perror("Error opening file");
// Return with non-zero exit status to
// indicate an error
return 1;
}
Open In App
if (ferror(file)) {
// Print an error message
// if an error occurred
// during file operations
printf("An error occurred while performing file "
"operations.\n");
}
6. Exit Status
Exit status is the value returned by the program after its execution is
completed which tells the status of the execution of the program.
Example
if (fp == NULL) {
printf("Value of errno: %d\n", errno);
printf("Error opening the file: %s\n",
strerror(errno));
perror("Error printed by perror");
exit(EXIT_FAILURE);
printf("I will not be printed\n");
}
else {
fclose(fp);
exit(EXIT_SUCCESS);
printf("I will not be printed\n");
}
return 0;
}
Output
Value of errno: 2
Error opening the file: No such file or directory
Error printed by perror: No such file or directory
Example
Open In App
C
void function(int);
int main()
{
int x = 0;
function(x);
return 0;
}
void function(int x)
{
float fx;
if (x == 0) {
printf("Division by Zero is not allowed");
fprintf(stderr, "Division by zero! Exiting...\n");
exit(EXIT_FAILURE);
}
else {
fx = 10 / x;
printf("f(x) is: %.5f", fx);
}
}
Output
BLACK FRIDAY OFFER! Subscribe for 1 Year and get 1 Extra year of
access completely FREE! Upgrade to GeeksforGeeks Premium today!