Error handling in C

 As such, C programming does now not supply direct aid for error managing however being a machine programming language, it affords you get right of entry to at decrease degree in the structure of return values. Most of the C or even Unix feature calls return -1 or NULL in case of any error and set an error code errno. It is set as a world variable and suggests an error befell throughout any characteristic call. You can locate a number error codes described in header file.

So a C programmer can check the lower back values and can take suitable motion relying on the return value. It is a desirable practice, to set errno to zero at the time of initializing a program. A fee of zero suggests that there is no error in the program.

errno, perror( ). and strerror( )

The C programming language presents perror() and strerror() features which can be used to show the textual content message related with errno.

  • The perror( ) characteristic shows the string you omit to it, accompanied by way of a colon, a space, and then the textual illustration of the contemporary errno value.

  • The strerror( ) function, which returns a pointer to the textual illustration of the cutting-edge errno value.

Let's strive to simulate an error circumstance and strive to open a file which does now not exist. Here I'm the usage of each the features to exhibit the usage, however you can use one or extra approaches of printing your errors. Second essential factor to be aware is that you have to use stderr file circulate to output all the errors.

        #include <stdio.h>
        #include <errno.h>
        #include <string.h>
        extern int errno ;
        int essential ( )
        {
            FILE * pf;
            int errnum;
            pf = fopen ("unexist.txt", "rb");
            if (pf == NULL)
            {
                errnum = errno;
                fprintf(stderr, "Value of errno: %d\n", errno);
                perror("Error printed by way of perror");
                fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
            } else
            {
                fclose (pf);
            }
            return 0;
        }

When the above code is compiled and executed, it produces the following end result −

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


Divide via Zero Errors

It is a frequent trouble that at the time of dividing any number, programmers do no longer take a look at if a divisor is zero and ultimately it creates a runtime error.

The code under fixes this by using checking if the divisor is zero earlier than dividing −


        #include <stdio.h>
        #include <stdlib.h>
        main( ) 
        {
            int dividend = 20;
            int divisor = 0;
            int quotient;
            if( divisor == 0)
            {
                fprintf(stderr, "Division by means of zero! Exiting...\n");
                exit(-1);
            }
            quotient = dividend / divisor;
            fprintf(stderr, "Value of quotient : %d\n", quotient );
            exit(0);
        }

When the above code is compiled and executed, it produces the following end result −


        Division by way of zero! Exiting...

Program Exit Status

It is a frequent exercise to exit with a price of EXIT_SUCCESS in case of application coming out after a profitable operation. Here, EXIT_SUCCESS is a macro and it is described as 0.

If you have an error situation in your application and you are coming out then you ought to exit with a popularity EXIT_FAILURE which is described as -1. So let's write above software as follows −


        #include <stdio.h>
        #include <stdlib.h>
        main( ) 
        {
            int dividend = 20;
            int divisor = 5;
            int quotient;    
            if( divisor == 0) 
            {
                    fprintf(stderr, "Division by way of zero! Exiting...\n");
                    exit(EXIT_FAILURE);
            }
            quotient = dividend / divisor;
            fprintf(stderr, "Value of quotient : %d\n", quotient );
            exit(EXIT_SUCCESS);
        }

When the above code is compiled and executed, it produces the following end result −

        Value of quotient : 4


Post a Comment

0 Comments