C Error Handling | Error Handling Concept in C Language

The C language, does not provide any Error handling or exception handling features like other high level languages. However, there are few other ways by which error handling can be done in C language.

There are some methods and variables defined in error.h header file can be used to point out error using the return statement in a function. In C language, the return value can be used to check error. There are a lot of functions that returns -1 or NULL value in case of any error and a global variable errno is set with the error code.

In C language, the programmer is expected to prevent the program from errors that occur in the first place, and test return values from functions.

Some of the methods and techniques used for Error handling C are :

1. errno()
2. strerror()
3. perror()
4. Exit Status

1. errno :

The errno is a global variable in C language, which can be used to identify which type of error was encountered while function execution, based on its value. The below shows a list of few different errno values :

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

To make use of errno, we have to include the header file errno.h. Now lets take a look at the use of errno in a C program :
#include <stdio.h>
#include <errno.h>

int main() {
  FILE *fp;
  fp = fopen("randomfile.txt", "r"); // file does not exists
  if(fp == NULL) {
    fprintf(stderr, "Value of errno is %d\n", errno);
  } else {
    fclose(fp);
  }
  return 0;
}
Output :

Value of errno is 2

At above example errno return value of 2, which means the file we want to open is not exists. Also note that we used stderr file stream with the fprintf() function to output all the errors. The stderr, also known as standard error, is the default file descriptor where a process can write error messages.

2. strerror() :

The strerror() function returns a pointer to the string representation of the represent errno value. The function is defined in the string.h, so we have to inclcude the header string.h in our C code. For example we can rewrite the above code code with strerror() function.
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
  FILE *fp;
  fp = fopen("randomfile.dat", "rb"); // file does not exists
  if(fp == NULL) {
    fprintf(stderr, "Value of errno is %d\n", errno);
    printf("Error Message : %s\n", strerror(errno));
  } else { 
    fclose(fp);
  }
}
Output :

Value of errno is 2
Error Message : No such file or directory

3. perror() :

The perror() function returns a string, which passed by the user followed by a colon, a space, and then textual representation of current errno value. The syntax of perror() is :
 perror("user supplied string");
Example :
#include <stdio.h>
#include <errno.h>

int main() {
  FILE *fp;
  fp = fopen("randomfile.dat", "rb"); // file does not exists
  if(fp == NULL) {
    fprintf(stderr, "Value of errno is %d\n", errno);
    perror("Error Message");
  } else { 
    fclose(fp);
  }
}
Output :

Value of errno is 2
Error Message: No such file or directory

4. Exit Status :

In programming,  it is a good practice to return a value if the program end successful or ends with an error. The C language provides two macros EXIT_SUCCESS and EXIT_FAILURE, which are defined in the stdlib.h header file. These two macros are used with the exit() function to inform about success or any error. The values of these two macros are :
 EXIT_SUCCESS 0
 EXIT_FAILURE 1
Example :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main() {
  FILE *fp;
  fp = fopen("randomfile.dat", "rb"); // file does not exists
  if(fp == NULL) {
    fprintf(stderr, "Value of errno is %d\n", errno);
    perror("Error Message");
    exit(EXIT_FAILURE);
  } else { 
    fclose(fp);
    exit(EXIT_SUCCESS);
  }
}
The above code gives below output

Output :

Value of errno is 2
Error Message: No such file or directory

And if we check the return value by the program then it is 1.
 # echo $?
 1
In Linux/Unix based system, we can check return values by 'echo $?' command.

Divide by Zero Error :

A common mistake made by C programmers is while dividing numbers, they did not check if a divisor is zero, which creates run-time error, and there nothing can be done to handle this type of error.

So to avoid this, first check the value of the divisor before using it in the division operations.

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

int main() {
  int dividend = 20;
  int divisor = 0;
  int quotient;

  if(divisor == 0){
    fprintf(stderr, "Error : Divide by zero.\n");
    exit(EXIT_FAILURE);
  }
   
  quotient = dividend / divisor;
  printf("Quotient : %d\n", quotient);
  exit(EXIT_SUCCESS);
}
Output :

Error : Divide by zero.

Next Topic :