codescracker


c

C Error Handling



« Previous Tutorial Next Tutorial »


C programming doesn't provide any direct support for the error handling but being a system programming language, it provides you to access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and sets an error code.

C developers should set errno to 0 at the time of initialization of the program. A value of 0 indicates that there is no error in the program.

C provides the two functions named perror() and strerror(), can be used to display the text message associated with the errno.

Handle Error in Opening a File in C

The following C program illustrates how to handle the error that occurs in opening a file.

/* C Error Handling - Error Handling Example Program */
		
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<conio.h>

extern int errno;

void main()
{
	FILE *fp;
	int errnum;
	char fname[20];
	clrscr();

	printf("Enter filename (not exist in the current directory): ");
	gets(fname);
	fp=fopen(fname, "r");
	if(!fp)
	{
		errnum = errno;
		fprintf(stderr, "Value of errno: %d\n", errno);
		perror("Error printed by perror");
		fprintf(stderr, "Error in opening file: %s\n", strerror( errnum ));
	}
	else
	{
		fclose(fp);
	}

	getch();
}

Here is the sample output of the above C program.

c error handling

Handle Divide by Zero Error in C

It is a good practice to check divide by zero error. Because sometimes, user might enter a value 0 to the denominator. Here this program illustrates the concept of checking the divide by zero error.

/* C Error Handling - This program illustrates
 * the concept of error handling in C programming
 */

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

void main()
{
	int numerator;
	int denominator;
	int quotient;
	clrscr();

	printf("Enter the value of numerator and denominator: ");
	scanf("%d%d", &numerator, &denominator);

	if(denominator == 0)
	{
		fprintf(stderr, "Divide by zero error..!!");
		printf("\nPress any key to exit...\n");
		getch();
		exit(-1);
	}
	quotient = numerator/denominator;
	fprintf(stderr, "Value of quotient: %d\n", quotient );

	getch();
}

Here is the two sample run of this C program. The first one for no error and the second one for divide by zero error.

error handling in c

c error handling example program

C Program Exit Status

There are the following two type of exit status:

Here, this C program illustrates the concept of program exit status i.e., EXIT_FAILURE and EXIT_SUCCESS.

/* C Error Handling - This program illustrates
 * the EXIT_SUCCESS and EXIT_FAILURE
 */

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

void main()
{
	int num;
	int den;
	int quot;
	clrscr();

	printf("Enter the value of numerator and denominator: ");
	scanf("%d%d", &num, &den);

	if(den == 0)
	{
		fprintf(stderr, "Divide by zero error..!!");
		printf("\nPress any key to exit...\n");
		getch();
		exit(EXIT_FAILURE);
	}
	quot = num/den;
	fprintf(stderr, "Value of quotient: %d\n", quot);

	printf("Press any key to exit...");
	getch();
	exit(EXIT_FAILURE);
}

On compile and executing the above code, it will produce the following result:

c error handling exit status

handle errors in c program

Now let's take a look at the mnemonics.

_doserrno

Variable indicating actual DOS error code. Here is the syntax to declare _doserrno:

int _doserrno

When an MS-DOS system call results in an error, _doserrno is set to the actual DOS error code. Here the following table lists the mnemonics for the actual DOS error codes which _doserrno can be set:

Mnemonic DOS error code
E2BIG Bad environ
EACCES Access denied
EACCES Bad access
EACCES Is current dir
EBADF Bad handle
EFAULT Reserved
EINVAL Bad data
EINVAL Bad function
EMFILE To many open
ENOENT No such file or directory
ENOEXEC Bad format
ENOMEM Mcb destroyed
ENOMEM Out of memory
ENOMEM Bad block
EXDEV Bad drive
EXDEV Not same device

errno

Variable indicating type of error. Here is the syntax to declare errno:

extern int errno;

Whenever an error in a system call occurs, errno is set to a value indicating the type of error. Here value can be:

Error numbers in errno

Here the following table lists the mnemonics and meanings for the error numbers found in errno. Each value listed can be used to index into the sys_errlist array for displaying messaged. Also, perror will display message.

Mnemonic Meaning
EZERO Error 0
EINVFNC Invalid function number
ENOFILE File not found
ENOPATH Path not found
ECONTR Memory blocks destroyed
EINVMEM Invalid memory block address
EINVENV Invalid environment
EINVFMT Invalid format
EINVACC Invalid access code
EINVDAT Invalid data
EINVDRV Invalid drive specified
ECURDIR Attempt to remove CurDir
ENOTSAM Not same device
ENMFILE No more files
ENOENT No such file or directory
EMFILE Too many open files
EACCES Permission denied
EBADF Bad file number
ENOMEM Not enough memory
ENODEV No such device
EINVAL Invalid argument
E2BIG Arg list too long
ENOEXEC Exec format error
EXDEV Cross-device link
EDOM Math argument
ERANGE Result too large
EFAULT Unknown error
EEXIST File already exists

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test