Error Handling in C – Learn to Deal with Exceptions

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.

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

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-

Implement Error Handling with Errno in C

Output-

Output of Error Handling with Errno in C

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-

Dealing with Exception Handling in C

Output-

Output of Exception Handling in C

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.