Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » C Programming » Tutorial » Error Handling

Error Handling in C Programming

C programming doesn't support error handling ( exception handling ) but there are fews ways to do error handling.

Developers need to prevent error during development cycle and should always test the return values of functions called by the program. Most of the C functions return -1 or NULL in case of an error. The returning numbers to indicate an error is mostly used in Unix or Linux like operating systems. Usually the program returns 0 when it runs successfully.

It is responsibility of C programm to do the error handling and make sure the program is termintes gracefully and not like unexpected crash.

The Global Variable errno

The global variable "errno" is integer variable which set set if there is any error during the function call. If we want to use errno in our code then include "errno.h" and put "extern int errno;" above main() function. Here is example -

Example on errno


#include<errno.h>
#include<stdio.h>

extern int errno;

int main(){
	FILE * fileptr;
	fileptr = fopne("cppbuzz.txt", "r");
	
	if(fileptr == NULL)
	    printf(" Value of errno is : %d \n", errno);
	else
	    fclose(fileptr);
return 0;
}

Ideally we should always use stderr file stream to output all of the errors, so the better code looks like -


#include<errno.h>
#include<stdio.h>

extern int errno;

int main(){
	FILE * fileptr;
	fileptr = fopne("cppbuzz.txt", "r");
	
	if(fileptr == NULL)
	    fprintf(stderr, " Value of errno is: %d \n", errno);
	else
	    fclose(fileptr);
return 0;
}

The output of the above both programs when the file "cppbuzz.txt" file doesn't exist.

Value of errno is : 2

Some Explanation
Header file stdio.h is used because we are using "FILE *fileptr", stderr & NULL in our code
Header file errno.h is used because we are using errno variable

The functions strerror() and perror()

The C programming provides strerror() and perror() library functions which can be used for printing error messages associated with errno.

strerror() function returns a pointer to the textual message of the current errno value
perror() function displays the string we pass to it, followed by a colon and the textual message of the current errno value.

Example on strerror() and perror()


#include <stdio.h>
#include <errno.h>
#include <string.h>

extern int errno;

int main () {
	FILE * fp;
	fp = fopen ("cppbuzz.txt", "rb");
	if (fp == NULL) {
		fprintf(stderr, "Value of errno: %d\n", errno);
		fprintf(stderr, "Error opening the file: %s\n", strerror( errno ));
		perror("Error printed by perror");
	} else {
		fclose (fp);
	}
	return 0;
}

Output of the program


    Value of errno: 2
    Error opening the file: No such file or directory
    Error printed by perror: No such file or directory

Example on Exit Status

It is always good practice to return a value if the program ends successfully or ends with an error.To achieve this we can the macros EXIT_FAILURE and EXIT_STATUS, these are defined in stdlib.h header file and it must be included in the program.

Example on Exit Status


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

extern int errno;

int main () {
	FILE * fp;
	fp = fopen ("filedoesnotexist.txt", "rb");
	if (fp == NULL) {
		fprintf(stderr, "Value of errno: %d\n", errno);
		fprintf(stderr, "Error opening the file: %s\n", strerror( errno ));
		perror("Error printed by perror");
		exit(EXIT_FAILURE);
	} else {
		fclose (fp);
		exit(EXIT_SUCCESS);
	}
	return 0;
}
Output of the program


# ./err
	Value of errno: 2
	Error opening the file: No such file or directory
	Error printed by perror: No such file or directory
	# echo $?
	1
	# touch filedoesnotexist.txt
	# ./err
	# echo $?
	0
	#

Please note the above program is tested on Linux. echo $? returns the exit status of last program.