Site icon DataFlair

Error Handling in C – Learn to Deal with Exceptions

Exception or Error Handling in C

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1. The incorporation of the header file <errno.h> simplifies things for us.

Functions Supporting Errno

The concept of error handling in C is based solely upon the header file <errno.h> and hence we will discuss the types of functions supported by it.

There are basically 2 types of functions associated with errno. They are namely, perror() and strerror(). We can use these functions to display the text message associated with errno.

  1. perror(): This function is responsible for displaying the string you pass to it, followed by a colon, a space, and then the textual representation of the current errno value.
  2. strerror(): This function is responsible for returning a pointer to the textual representation of the current errno value.

It’s the right time to explore the concept of Functions in C/C++

How to Implement Error Handling with Errno in C?

Here is a simple program that will help you understand how exception or error handling is done in C with the help of errno.
This program shows what happens when we try to open a file that does not exist in the computer system.

Here is a code in C that illustrates the use of errno:

#include<stdio.h>
#include<errno.h>
int main() 
{ 
FILE *fpointer; 

fpointer = fopen("DataFlair.txt", "r"); // Opening the file 
printf(" The value of errno is: %d\n", errno); 

return 0; 
}

Code-

Output-

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Dividing by Zero Problem

It is the most popular problem discussed when dealing with error or exceptions.

We often give mathematical statements like:

c = a / b;

But we often tend to forget that in certain cases, the value of b is likely to be 0.

Mathematics defies the division of a number by 0. People often misconceive the answer to be infinite. Well, it is not the case. The answer, in this case, is “not defined”.

Here is a program that helps you deal with this problem by applying the concepts of exception or error handling in C:

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

int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int dividend = 43;
int divisor = 0;
int quotient;

if( divisor == 0)
{
fprintf(stderr, "Division by zero is not possible!\n");
exit(-1);
}

quotient = dividend / divisor;
fprintf(stderr, "The Value of quotient : %d\n", quotient );
exit(0);
return 0;
}

Code-

Output-

Quiz on Error Handling in C

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    #include <stdio.h>

    #include <errno.h>

     

    int main () {

     

        FILE * file;

        file = fopen (“unexist.txt”, “r”);

        fprintf(stderr, “%d\n”, errno);

        return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    #include <stdio.h>

    #include <errno.h>

     

    int main () {

     

        FILE * file;

        file = fopen (“unexist.txt”, “r”);

        perror(“Error”);

        return 0;

    }

     

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    #include <stdio.h>

    #include <errno.h>

     

    int main () {

     

        FILE * file;

        file = fopen (“unexist.txt”, “r”);

        fprintf(stderr, “Error: %d”, strerror(errno));

        return 0;

    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    #include <stdio.h>

    #include <errno.h>

    #include <string.h>

     

    int main () {

     

        FILE * file;

        file = fopen (“unexist.txt”, “r”);

        fprintf(stderr, “Error: %s”, strerror(errno));

        return 0;

    }

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    #include <stdio.h>

    #include <stdlib.h>

     

    int main() {

     

       int dividend = 10;

       int divisor = 5;

       int quotient;

     

       if( divisor == 0) {

          exit(EXIT_FAILURE);

       }

       exit(EXIT_SUCCESS);

       quotient = dividend / divisor;

       printf(“%d\n”, quotient);

    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    #include <stdio.h>

    #include <stdlib.h>

     

    int main() {

     

       int dividend = 10;

       int divisor = 5;

       int quotient;

     

       if( divisor == 0) {

          exit(EXIT_FAILURE);

       }

       

       quotient = dividend / divisor;

       printf(“%d\n”, quotient);

       exit(EXIT_SUCCESS);

       

    }

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    #include <stdio.h>

    #include <stdlib.h>

     

    int main() {

     

       int dividend = 10;

       int divisor = 5;

       int quotient;

     

       if( divisor == 0) {

          printf(“%d”,EXIT_FAILURE);

          exit(EXIT_FAILURE);

       }

       

       quotient = dividend / divisor;

       printf(“%d”,EXIT_SUCCESS);

       exit(EXIT_SUCCESS);

    }

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    #include <stdio.h>

    #include <stdlib.h>

     

    int main() {

     

       int dividend = 10;

       int divisor = 0;

       int quotient;

     

       if( divisor == 0) {

          printf(“%d”,EXIT_FAILURE);

          exit(EXIT_FAILURE);

       }

       

       quotient = dividend / divisor;

       printf(“%d”,EXIT_SUCCESS);

       exit(EXIT_SUCCESS);

    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    #include <stdio.h>

    #include <errno.h>

    #include <stdlib.h>

     

    int main() {

     

       int a[10];

       int j=20;

       int k=0;

       for(int i=0;i<=j;i++)

       {

           k++;

           a[i]=k;

           if(i>10)

           {

            perror(“Error”);

            exit(EXIT_FAILURE);

           }

       }

       exit(EXIT_SUCCESS);

    }

     

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    #include <stdio.h>

    #include <errno.h>

    #include <stdlib.h>

     

    int main() {

     

       int a[10];

       int j=20;

       int k=0;

       for(int i=0;i<=j;i++)

       {

           k++;

           a[i]=k;

           if(i>10)

           {

            exit(EXIT_FAILURE);

           }

       }

       perror(“Error”);

       exit(EXIT_SUCCESS);

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    Which among the following header files contains predefined error code?

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    Which of the following functions can be used to display the current errno value?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    Which of the following functions can be used to return the pointer to the textile representation of the current errno value?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    Which file stream is used to output all the errors?

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    What is not true about errno?

    Correct
    Incorrect

Summary

In this tutorial, we discussed the meaning of exception or error handling in C. We inferred that it is not supported in the C programming language and hence we use the header file <errno.h> while dealing with such situations. Then, we saw the 2 types of functions associated with errno. Lastly, we concluded our discussion with a popular case where we learned how to tackle situations where there is a division of a number by zero with the help of a simple program.

Follow these best practices for C programming – Must know to become an Expert

Hope you liked our explanation. Any queries? Feel free to share in the comment section.

Exit mobile version