Error handling in c

What is error handling in c programming?

There are no direct support for error handling in c. C provides an access at lower level to handle errors.

Most of the function calls return -1 or NULL in case of any errors and sets errno i.e. error code. One can find various error codes in error.h header file. So c programmer can check the errors depending on error values and can take action.

In c programming, we can make use of return values to handle the error. We have to check for errors based on the return values of functions.


Different methods of error handling in c

Below is the list of different methods to handle errors in c programming.

  • Global variable errno.
  • perror() and strerror() functions.
  • Exit status.
  • Manual error handling (such as divide by zero in c).

Global variable errno

When a function call occurs in c a global variable errno is set a value which can be used to find out errors. The errors are defined in errno.h header file.

Following are the different types of errorno values in c.

errno valueMeaning
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

Example

#include<stdio.h>
#include<errno.h>
int main()
{
  FILE *fpointer;
  fpointer=fopen("Schoalrsoul.txt","r");
  printf("Error is %d \n",errno);
  return 0;
}
	Error is 2 
Error handling in c through Global variable errno method

In the above example, we tried to open a file that doesn’t exist. This set the errorno global variable value to 2 which is no such file or directory.


perror and strerror function

These methods are used to display error message that are associated with errno.

perror function

perror function displays string that is passed by user.

Syntax

void perror(const char *string);

Example

#include <stdio.h>
#include<stdlib.h>
int main () {
   FILE *fp;
   //trying to open a file that doesnt exist 
   fp = fopen("scholarsoul.txt", "r");
   if( fp == NULL ) {
      perror("\n\n\tError couldnot open file. ");
       printf("\n\n");
      return(-1);
   }
   fclose(fp);
   return(0);
}
	Error couldnot open file. : No such file or directory
Error handling in c through perror function

strerror function

strerror function points to the textual message of current errorno value.

Syntax

char *strerror(int errnum);

Example

#include<stdio.h>
#include<string.h>
#include<errno.h>
int main()
{
 FILE *fpointer;
 fpointer=fopen("Scholarsoul.txt","r");
 printf("Error num:%d \n",errno);
 printf("The error message:%s \n",strerror(errno));
 return 0;
}
        Error num:2 
	The error message:No such file or directory 
Error handling in c through strerror function

Exit status

It specifies EXIT_SUCCESS and EXIT_FAILURE that are passed to exit() function to indicate status on termination. These macros are defined in stdlib.h header file.

Example

#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
int main()
{
  FILE *fpointer;
  fpointer=fopen("scholarsoul.txt","r");
  if(fpointer==NULL)
  {
    perror("Error message");
    exit(EXIT_FAILURE);
  }
  else
  {
   fclose(fpointer);
   exit(EXIT_SUCCESS);
  }
  return 0;
}
Error message: No such file or directory
Error handling by exit status in c programming

Divide by zero errors

C does not provide any construct to check divide by zero errors. It creates runtime error.

It is the responsibility of the programmer to check the divisor is non zero elements. Checking whether divisor is zero or not is a good programming practice.

Dividing by zero may cause undefined behaviour.

Example

#include<stdio.h>
int main()
{
 int a,b,res;
 printf("Enter a and b values \n");
 scanf("%d %d ",&a,&b);
 if(b==0)
   printf("Divide by zero error \n");
 else 
   {
    res=a/b;
    printf("result is %d \n",res);
   }
 return 0;
}
Enter a and b values 
	1  

	0 
	Divide by zero error 
Handling divide by zero error in c programming

People also ask for

What is error handling in c?

C does not provide direct support to handle errors. It provides an access at lower level to handle errors.

Does C support exception handling?

C does not provide direct means of error handling technique but it can achieved by c programmer by c constructs.


People also read