How to Handle errors in C Language

Error handling in C is not directly supported by C programming, but as a system programming language, it does give you access at a lower level in the form of return values.

Most C or even Unix function calls return -1 or NULL and set an error code errno in the event of an error.

A function call error is indicated by this global variable, which is set. In the header file “error.h”, a number of error codes are defined.

Overview

The functional methods perror(), strerror(), ferror(), feof(), clearerr(), and Exit status are used to handle errors while executing file operations even though the C language lacks direct support for error handling.

When we attempt to use a file that has not yet been opened or read a file that doesn’t even exist, issues frequently occur.

We can consult errno to determine the type of error. When we call a function, it is a global variable that is automatically populated with a numeric value.

Scope of Article

  • This article describes the many error types that we run into when working with files in C.

  • We learn about errno values and how to correctly handle problems by just understanding the errno that goes along with them.

  • We also learn about various functional ways to deal with problems when carrying out file handling activities in C.

Functions Supporting Errno

We will explore the kinds of functions that are provided by the header file errno.h because that is the only place where the concept of error handling in C is built.

There are really only two categories of errno-related functions. They are perror() and strerror(), respectively.

These routines allow us to show the text message related to errno.

  • The textual representation of the current errno value is displayed after the colon, space, and string you supply to the function perror(), which is then responsible for displaying the string you passed it.

  • In order to retrieve the textual representation of the current errno value, use the strerror() method.

How to Implement Error Handling with Errno in C?

Here is an easy system that will show you how to use errno to handle exceptions and errors in C.

This application demonstrates what occurs when a file that does not exist in the computer system is attempted to be opened.

errno valueError
1Operation not permitted
2No such file or directory
3No such process
4Interrupted system call
5I/O error
6No such device or address
7Argument list too long
8Exec format error
9Bad file number
10No child processes
11Try again
12Out of memory
13Permission denied

Here is some C code that demonstrates how to use errno:

#include<stdio.h>
#include<errno.h>
int main() 
{ 
FILE *fpointer; 

fpointer = fopen("tutorial.txt", "r"); // Opening the file 
printf(" The value of errno is: %d\n", errno); 

return 0; 
}

Output of the above example:

The value of errno is: 2

You can test the above example here! ➡ C Online Compiler

Divide by Zero Errors

Programmers frequently fail to check whether a divisor is 0 when dividing any number, which results in runtime errors.

The following code corrects this by determining whether the divisor is 0 before dividing.

#include <stdio.h>
#include <stdlib.h>

int main()
{

printf("Welcome to C Programming tutorials!\n\n");

int dividend = 69;
int divisor = 0;
int quotient;

if( divisor == 0)
{
fprintf(stderr, "Division by zero is not possible!\n");
exit(-1);
}

quotient = dividend / divisor;
fprintf(stderr, "The Value of quotient : %d\n", quotient );
exit(0);
return 0;
}

Output of the above example:

Welcome to C Programming tutorials!
Division by zero is not possible!
Command exited with non-zero status 255

You can test the above example here! ➡ C Online Compiler

Program Exit Status

It is standard practice to end a program after a successful operation using the value EXIT SUCCESS.

Here, the macro EXIT SUCCESS is declared as 0.

When leaving a program with an error situation, you should quit with the state EXIT FAILURE, which is defined as -1.

Then let’s write the program as follows:

#include <stdio.h>
#include <stdlib.h>

int main() {

   int dividend = 45;
   int divisor = 9;
   int quotient;
 
   if( divisor == 0) {
      fprintf(stderr, "Division by zero! Exiting...\n");
      exit(EXIT_FAILURE);
   }
	
   quotient = dividend / divisor;
   fprintf(stderr, "Value of quotient : %d\n", quotient );

   exit(EXIT_SUCCESS);
}

Output of the above example:

Value of quotient : 5

You can test the above example here! ➡ C Online Compiler

Conclusion

  • When we call a function in the C programming language, the variable errno is given a numeric value, and we can use it to determine the type of error we experienced while creating the code.

  • Some frequent mistakes that may occur when doing file handling operations in C include reading a file that doesn’t even exist or using a file that hasn’t even been opened.

  • perror(), strerror(), ferror(), feof(), clearerr(), and Exit status are some helpful functional functions for error handling in the C language that can assist you prevent errors when doing file operations.

About Next Tutorial

In the following post, I’ll create a Recursion in C and try to explain its many components in full detail.

I hope you enjoy this post on Error Handling During File Operations in C Language, in which I attempt to illustrate the language’s core grammar.


Leave a Comment