Error, Handling, C programming

Error Handling

Suppose we are dividing two numbers and one of them is zero. In this case the program will not automatically handle the error of dividing by zero. The user needs to explicitly check for the numbers and display proper message. We need to send -1 or NULL value to the calling program or operating system. C does not provide any exception handling or error handling features like any other programming language. But C allows error handling by using the variables and functions defined in the header file “errno.h”. It has error code in a variable ‘errno’ – a global variable and contains different error codes for different errors. Hence at the beginning of the program, errno is initialized to zero to indicate that it does not reflect any unwanted errors.

extern int errno;

Below is the simple program to capture the error number while opening a file which does not exists. This error number is defined in the header file errno.h, and is printed using printf function. It throws an error with number 2.

 

#include 
#include 

void main() {
	FILE *fp;
	 
	fp = fopen("ErrorTesting.c", "r"); // Open the file which does not exists, so that error can occur
	if (fp)
		printf("Error has Occurred with Error Number: %d", errno); //prints the error number
	else
		fclose(fp); //if file exists, close the file
}

 

In above case, we can see the error number, but any user seeing this number cannot understand what error is it. If we display the error message associated with the error number or some meaningful description about the error that is encountered, then it is helpful for anyone seeing the output. Depending on the errno, C provides two functions to display the error message to the user.

strerror ()

This function displays the error message corresponding to the error number that is stored in errno.h header file. In other words, this function points to the message corresponding to the errno. All these error messages are printed to the standard error file stream – stderr to get the error outputs.

#include 
#include 

void main() {
	FILE *fp;
	
	printf("\nTrying to open a file ErrorTesting.c\n");
	fp = fopen("ErrorTesting.c", "r"); // Open the file which does not exists, so that error can occur
	if (fp)
		fprintf(stderr, "%s", strerror(errno)); //prints the error message pointed by errno
	else
		fclose(fp); //if file exists, close the file
	}

Here we can give user messages while printing the error message like below:

fprintf(stderr, “Error Message Pointed by errno is :%s”, strerror(errno));

perror ()

This is similar to strerror function, but this function prints the user description about the error that user passes to it followed by a colon and the error message pointed by the errno. Here no need to explicitly call a function by passing the error number to get the actual error message like we do in strerror.

perror(“Error while opening a file”);

#include 
#include 

void main() {
	FILE *fp; 
	
	printf("\nTrying to open a file ErrorTesting.c\n");
	fp = fopen("ErrorTesting.c", "r"); // Open the file which does not exists, so that error can occur
	 
	 if (fp)
		perror("Error while opening a file"); // prints this user defined message followed by colon and error description pointed by errno
	else
		fclose(fp); //if file exists, close the file
}

Whenever an error occurs in the program, we use either of the functions above and should exit from the program. Usually it is tendency to exit with status as 1 for failure and as 0 for success. In order to avoid hard coding of exit status and not to confuse with zero and one, C provides a macro which holds the values for success and failure. These macros are defined in stdlib.h header file (standard library file).

#define EXIT_SUCCESS 0 // exit status for successful completion defined in stdlib.h
#define EXIT_FAILURE 1 // exit status for failure defined in stdlib.h

Thus in above programs, on displaying error messages, we need to exit from the program by calling the right macros like below:

#include 
#include 
#include 

void main() {
	FILE *fp;

	printf("\nTrying to open a file ErrorTesting.c\n");
	fp = fopen("ErrorTesting.c", "r"); // Open the file which does not exists, so that error can occur
	if (fp){
		fprintf(stderr, "%s", strerror(errno)); //prints the error message pointed by errno
		exit(EXIT_FAILURE);// exit status is one
	}else{
		fclose(fp); //if file exists, close the file
		exit(EXIT_SUCCESS);// exit status is zero
	}
}
#include 
#include 
#include 

void main() {
	FILE *fp;

	printf("\nTrying to open a file ErrorTesting.c\n");
	fp = fopen("ErrorTesting.c", "r"); // Open the file which does not exists, so that error can occur

	if (fp){
		perror("Error while opening a file"); // prints this user defined message followed by colon and error description pointed by errno
		exit(EXIT_FAILURE);// exit status is one
	}else{
		fclose(fp); //if file exists, close the file
		exit(EXIT_SUCCESS);// exit status is zero
	}
}
Translate »