C Error Handling

Introduction to C Error Handling

Errors किसी भी program में आ सकती है। इन्हें exceptions भी कहा जाता है। ये वो errors होती है जो program के run होते हुए समय आती है। इनके आने से program का execution रुक जाता है। C programming error handling को support नहीं करती है।

लेकिन c program में errors को आप return values से पहचान सकते है। जब भी C program में कोई exception आती है तो functions -1 या NULL value return करते है। इन values को पहचान कर आप पता लगा सकते है की error आयी है या नहीं।

errno Variable

C programming आपको errno नाम का एक global variable provide करती है। जब भी program में कोई exception आती है तो इस variable को error number के साथ set कर दिया जाता है। एक error number <error.h> header file में एक particular error को represent करता है। सभी errors इसी header file में define की गयी है।

इस variable को आपको अपने program में यूज़ करने के लिए इस तरह define करना चाहिए।

extern int errno;

इस variable को initially zero के साथ define करना और भी अच्छा माना जाता है। क्योंकि zero का मतलब होता है की कोई error नहीं है। साथ ही आपको <errno.h> header file को भी अपने program में include करना पड़ेगा।

Functions for Error Handling

C language आपको errors को represent करने के लिए 2 functions provide करती है।

  • perror() – ये function आपके द्वारा pass की गयी string को print करता है और साथ ही जो error generate हुई है उसका description print किया जाता है। इस function से ये पता चल जाता है की कौनसी error आयी है।
  • strerror() – ये function generate की गयी error के textual representation का pointer return करता है। ये function standard errors को point करता है।
#include <stdio.h>
#include <errno.h>
extern int errno;
int main()
{
    FILE *fp;
    fp = fopen(“test.txt”,”r”);
    /* Checking for error, whether file is loaded or not */

    if(fp == NULL)
    {   
         /* Printing error number & description */
         fprintf(stderr,”Error Number :  %dn”,errno);
         perror(“Error Description : “);
    }
   else
    {
        fclose(fp);
    }
}   
Error Number : 2
Error Description : No such file or directory

exit() Function

जब भी आपका program execute होता है तो आप exit status return कर सकते है। ये status बताता है की आपका program successfully execute हुआ है या error आने से terminate हुआ है। Exit function के और भी यूज़ है लेकिन यँहा पर errors के संदर्भ में आप इसे 2 तरह से यूज़ कर सकते है।

यदि आपका program किसी error की वजह से terminate हो रहा है तो आप exit function को -1 या EXIT_FAILURE string के साथ call कर सकते है। यदि आपका program बिना किसी error के successfully terminate हो रहा है तो आप exit function को 0 या EXIT_SUCCESS string के साथ call कर सकते है।

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
extern int errno;
int main()
{
    FILE *fp;
    fp=fopen(“test.txt”,”r”);
    /* Checking for error */

    if(fp == NULL)
    {
        fprintf(stderr,”Error Number : %d”, errno);
        perror(“Error Description “);
        fprintf(stderr,”Exiting due to error….”);

        /* Exiting program due to error */
        exit(-1);
    }
    else
    {
        fclose(fp);
    }
}
Error Number : 2
Error Description : No such file or directory
Exiting due to error...