Error Handling in C

Error Handling in C Program with Examples

In this article, I am going to discuss Error Handling in C Program with Examples. Please read our previous article where we discussed File Handling in C Program. Handling Error is the most important thing in all programming languages. At the end of this article, you will understand how to handle errors in the C programming language.

Error Handling in C Program:

There is no direct support for Error Handling in C language. However, using the return statement in a function, there are few methods and variables defined in error.h header file that can be used to point out the error.  A programmer has to prevent errors in the first place before testing return values from the functions.  In C, the function return NULL or -1 value in case of any error. There is a global variable errno which sets the error code/number. Hence, while programming the return value can be used to check error.

What is errno?

When a function is called in C, a variable named errno is automatically assigned a code (value) which can be used to identify the type of error that has been encountered. It is a global variable indicating the error occurred during any function call and defined in the header file errno.h.

List of few different errno values and its corresponding meaning:

errno values and its corresponding meaning in C Language

Example:
#include <stdio.h>
#include <errno.h>
int main ()
{
    FILE *fp;
    fp = fopen ("File.txt", "r");
    printf (" Value of errno: %d\n ", errno);
    return 0;
}

Output:

Error Handling in C Program

Functions used to display a text message associated with errorno

perror(): It returns the string you pass to it, followed by a colon, a space, and then the textual representation of the current errno value.

Syntax: void perror (const char *str) where str is a string containing a message to be printed.

strerror(): It returns a pointer to the textual representation of the current errno value.

Syntax: char *strerror (int errnum) where errnum is the error number.

Example
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main ()
{
    FILE *fp;
    fp = fopen (" File.txt ", "r");
    printf ("Value of errno: %d\n", errno);
    printf ("The error message is : %s\n", strerror (errno));
    perror ("Message from perror");
    return 0;
}

Output:

How to Handle Error in C Programming Language

Divide by Zero Error in C Program

Sometimes at the time of dividing any number, programmers do not check if a divisor is zero and finally it creates a runtime error. Because C Language cannot do anything about it. All you can do is, avoid it.

Example
#include<stdio.h>
#include <stdlib.h>
void function (int);
int main ()
{
    int x = 0;
    function (x);
    return 0;
}

void function (int x)
{
    float fx;
    if (x == 0)
    {
        printf ("Division by Zero is not allowed \n");
        fprintf (stderr, "Division by zero! Exiting...\n");
        exit (EXIT_FAILURE);
    }
    else
    {
        fx = 10 / x;
        printf ("f(x) is: %.5f", fx);
    }
}

Output:

Divide by Zero Error in C Program

Exit Status

The Exit Status constants are used in the exit() function to inform the calling function about the error. EXIT_SUCCESS and EXIT_FAILURE are the two constant values available for use. These are macros defined in stdlib.h which indicates successful or unsuccessful termination, respectively.

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

int main ()
{
    FILE *fp;
    fp = fopen ("filedoesnotexist.txt", "rb");

    if (fp == NULL)
    {
        printf ("Value of errno: %d\n", errno);
        printf ("Error opening the file: %s\n", strerror (errno));
        perror ("Error printed by perror");
        exit (EXIT_FAILURE);
        printf ("I will not be printed\n");
    }
    else
    {
        fclose (fp);
        exit (EXIT_SUCCESS);
        printf ("I will not be printed\n");
    }
    return 0;
}

Output:

Error Handling in C Program with Examples

Here, in this article, I try to explain Error Handling in C Program with Examples. I hope you enjoy this Error Handling in C Language article. I would like to have your feedback. Please post your feedback, question, or comments about this Error Handling in C article.

Leave a Reply

Your email address will not be published.