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.
- 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.
- 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
0 of 15 Questions completed Questions: 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:
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) #include <stdio.h> #include <errno.h> int main () { FILE * file; file = fopen (“unexist.txt”, “r”); fprintf(stderr, “%d\n”, errno); return 0; } #include <stdio.h> #include <errno.h> int main () { FILE * file; file = fopen (“unexist.txt”, “r”); perror(“Error”); return 0; } #include <stdio.h> #include <errno.h> int main () { FILE * file; file = fopen (“unexist.txt”, “r”); fprintf(stderr, “Error: %d”, strerror(errno)); return 0; } #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; } #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); } #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); } #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); } #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); } #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); } #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); } Which among the following header files contains predefined error code? Which of the following functions can be used to display the current errno value? Which of the following functions can be used to return the pointer to the textile representation of the current errno value? Which file stream is used to output all the errors? What is not true about errno?
Quiz Summary
Information
Results
Results
0 Essay(s) Pending (Possible Point(s): 0)
Categories
1. Question
2. Question
3. Question
4. Question
5. Question
6. Question
7. Question
8. Question
9. Question
10. Question
11. Question
12. Question
13. Question
14. Question
15. Question
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.