Thursday, April 25, 2024
HomeC Programmingexception dealing with in c

exception dealing with in c


What’s Exception Dealing with?

Every time we create any software/software program in any language(Java,C,C++,Python and so on) then there are possibilities of Runtime error which can happen even when the software program is in manufacturing stage,So such kind points  are referred to as runtime error or Exception,So it turn into important to handle such kind of error in any other case it would disturb the traditional circulation of software program/software.

I’ll give one instance from that you’re going to get clear thought about it,Lets suppose we’ve created one Calculator Utility  right here we’ve built-in fundamentals arithmetic capabilities like Addition,Subtraction,Multiplication,Division.Right here if person by mistakenly  performing division operation of any quantity with 0 then it would increase an Exception and program execution will cease since it’s not doable to divide any error with zero So to handle  this kind of points we have to use Exception Dealing with idea.

Among the commonest Exception are as comply with:

FileNotFoundException

DividebyZeroError

NullPointerExcption

MemoryError

 Until the date,C Programming doesn’t present the exception dealing with or error dealing with straight however we will obtain it utilizing one of many header file(errorno.h) offered by C.errorno.h header file comprises all  runtime error which can happen in the course of the execution of c program and it has distinctive errorno related to it.

With a view to give the error message to person,A Developer has to forestall error at first place and return values from the perform.A lot of the C Perform calls return -1 or NULL in case of an error

Following are Among the widespread error and its description :

errno worth       Error

1             /* Operation not carried out */

2             /* No such file or listing */

3             /* No such course of */

4             /* Interrupted system name */

5             /* Enter/Output error */

6             /* No such system or deal with */

7             /* Argument listing too lengthy */

8             /* Execution format error */

9             /* Dangerous file quantity */

10            /* No little one processes */

11            /* Attempt once more */

12            /* Out of reminiscence */

13            /* Permission denied */

Shifting forward,we are going to first this system on see how error values are set if program discovered any error throughout it is execution.

predominant.c 

#embrace <stdio.h>

#embrace<errno.h>

int predominant()

{

    FILE * f;

    f = fopen(“inspiredcoder.txt”, “r”);

  

    printf(” Worth of Error Quantity: %dn “, errno);

  

    return 0;

}

Output-Worth of Error Quantity: 2

In above program,the error worth is 2 as a result of In my present listing the place my predominant.c resides there was no file of identify inspiredcoder.txt and we already noticed within the errono’s desk  “No such file or listing” message is related to error quantity 2.Some on-line compiler will give error no-13  which says permission denied.One necessary to notice right here is that errno is a key phrase which is used to point out error messages.

In-built strategies which can assist us to attain Exception dealing with 

As of now we’re solely capable of determine the error quantity what if we need to present error message’s accordingly.To beat this challenge we’ve 2 technique’s they resides in errno.h header file,they’re perror() and strerror().

perror():This perform show the string you go to it,adopted by a colon,an area after which the textual illustration of the present error worth. 

strerror():this perform returns a pointer to the textual illustration of the present errno worth.

Syntax  of perror() Perform:

void perror(const char *string)

string:We will go any customized message which we need to print earlier than the error message itself,

Syntax  of strerror() Perform:

char *strerror (int errnum)

errnum: is the error quantity (errno).

C Program to see how sterror() and perror() message are used to print the error messages 

// On-line C compiler to run C program on-line

#embrace <stdio.h>

#embrace <errno.h>

#embrace <string.h>

  

int predominant ()

{

    FILE *f;

    f = fopen(” inspiredcoder.txt “, “r”);

      printf(“The error message is : %sn”, strerror(errno));

    perror(“Message from perror”);

    

  

    return 0;

}

Output-The error message is : No such file or listing

             Message from perror: No such file or listing

In above program,we’ve used strerror() during which we’re passing errorno as a parameter which can assist strerror() to point out error message accordingly.Within the subsequent line we’re perror message which can print the error message robotically which simply must go any customized message earlier than the error message,In above instance we’ve handed  ‘Message from perror’ as customized error message.

Exit Standing:C commonplace Specifies two commonplace constants they’re EXIT_SUCCESS and EXIT_FAILURE,they combinly can  be used to attain catch or besides block(Like we used to do in Python or Java),Since until now we’re solely capable of acknowledge error and print it,however now we’ve to verify as soon as error is encountered then we’ve to handle the remaining circulation of this system,So to do it we’re going to use EXIT_FAILURE and EXIT_SUCCESS constants,these are macros outlined in stdlib.h

#embrace <stdio.h>

#embrace <errno.h>

#embrace <string.h>

#embrace <stdlib.h>

  

int predominant ()

{

    FILE * f;

    f = fopen (“filedoesnotexist.txt”, “rb”);

  

    if (f == NULL)

    {

        printf(“Worth of Error Quantity: %dn”, errno);

        printf(“Error whereas opening the file: %sn”,

                             strerror(errno));

        perror(“Error printed by perror perform”);

  

        exit(EXIT_FAILURE);

        printf(“I’m not going to be printedn”);

    }

  

    else

    {

        fclose (f);

        exit(EXIT_SUCCESS);

        printf(“I can’t be Printedn”);

    }

    return 0;

}

Output-Worth of Error Quantity: 2

Error whereas opening the file: No such file or listing

Error printed by perror perform: No such file or listing

In above program we will see that we’ve used EXIT_FAILURE and EXIT_SUCCESS Constants,from above program we get to identified that we’ve to make use of EXIT_FAILURE constants  when we’ve encountered the error and need to proceed to the subsequent step for managing error,principally each time the EXIT_FAILURE constants is discovered handed in an exit() perform argument then this system will pause the execution of that specific block(eg. if block). and proceed to the subsequent part of the code.

Equally EXIT_SUCCESS constants will likely be used to point out profitable termination of program.

Should you any have doubt,suggestion relating to this put up or web site then be happy to share with us.

 

In case you have discovered one thing new from this put up then share this put up with your loved ones and
associates.

 

Pleased Studying :)😎  

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments