Error Handling in C
As we know that C programming language offers various features and functionalities to the programmers. The syntax of this language is easy to understand and easy to implement.
C does not provide direct error handling features but you can do error handling in C through different ways. A good and skilled programmer has to avoid the errors and he also has to test the return values from the functions.
What is Error Handling in C?
As we said that you cannot do error handling in C directly. But there are ways through which you can perform error handling. Through error handling, you can check the return values from functions and should take appropriate action depending on the return value.
In C, a lot of function calls return a -1 or NULL value in case of an error and it sets an error code errno. It indicates that an error has occurred during a function call.
In Socket programming, the return value of functions such as socket(), listen() etc. are checked to identify if an error is occured or not.
Example:- Socket Programming
if ((connect = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("Connection failed!"); exit(); }
Different ways to handle the errors in C:-
1. errno in c:-
It is a global variable and it sets the error code. In C, when a function is called then errno is assigned a code or number automatically and this can be used to analyze which type of error is encountered. It is defined in the errno.h header file.
Following is the list of different errno values:-
Errno Value | What’s the error |
1 | The operation is not permitted. |
2 | No such file or directory. |
3 | No such process. |
4 | Interrupt system call. |
5 | Input/Output error. |
6 | No such device or address. |
7 | The argument list is too long. |
8 | Exec format error. |
9 | Bad file number. |
10 | No child process. |
11 | Try again. |
12 | Out of memory. |
13 | Permission denied. |
Example:- errno
#include <stdio.h> #include <errno.h> int main() { FILE *file_name; file_name = fopen("TechVidvan.txt", "r"); printf("Value of errno: %d\n ", errno); return 0; }
Output:-
In the above example, if the file ‘TechVidvan.txt’ file does not exist then it will return you an error. And the value of errno will be printed which is 2 (No such file or directory).
2. perror() and strerror() in C:-
If you want to display the error description then you can make use of two functions which can be used to show a message that is associated with errno value. Below are the two functions:-
a. perror:- With the help of this, you can display a string which you will pass to it. It is followed by a colon, a space and the textual representation of the errno value.
void perror (char *s) s: a custom message to be printed before the error message.
b. trerror:- It is mainly used for returning a pointer to the textual representation of the errno value.
char *strerror (int error_number) error_number: represents the error number (errno).
Example:- strerror() and perror()
#include <stdio.h> #include <errno.h> #include <string.h> int main () { FILE *file_name; file_name = fopen("TechVidvan.txt", "r"); printf("The value of errno is %d!\n", errno); printf("Error message: %s\n", strerror(errno)); // using strerror! perror("perror"); // using perror! return 0; }
Output:-
The value of errno is 2!
Error message: No such file or directory
3. Exit Status in C:-
In C, you can also make use of the exit status constants in the exit() function to indicate successful or unsuccessful termination of the program code.
There are two constants such as EXIT_SUCCESS and EXIT_FAILURE. These are macros which are defined in the stdlib.h header file.
Example:- Exit Status
#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h> int main () { FILE *file_name; file_name = fopen ("TechVidvan.txt", "rb"); if (file_name == NULL) { printf("The value of errno is %d!\n", errno); printf("The error is: %s\n", strerror(errno)); exit(EXIT_FAILURE); } else { fclose(file_name); exit(EXIT_SUCCESS); } return 0; }
Output:-
The error is: No such file or directory
In the above example, if the file ‘TechVidvan.txt’ exists then it will exit with success. And if it does not exist then it will exit with failure and it will display the errno value and an error message saying No such file or directory.
4. Divide by zero errors in C:-
In this situation, you cannot handle the error. Division by zero will lead to undefined behaviour. All you can do is to check the value of the divisor before using it in the division.
You can make use of the if condition. If the value of division is zero then simply display a message and return from the function.
Example:- Divide by zero error
#include<stdio.h> #include <stdlib.h> void divide_zero_error(int); int main() { int num = 0; divide_zero_error(num); return 0; } void divide_zero_error(int num) { float num1; if (num==0) { printf("You are dividing a number by zero! You will face an error!\n"); fprintf(stderr, "Division by zero!\n"); exit(EXIT_FAILURE); } else { num1 = 10 / num; printf("Value is: %f", num1); } }
Output:-
You are dividing a number by zero! You will face an error!
If you run the above code then it will print the error message that you have put. Because you are dividing a number by zero.
Summary
In this tutorial, we learnt about error handling in C. We discussed what error handling is in C and different ways to handle the errors in C in detail.
We also discussed functions such as perror() and strerror(). Then we talked over how you can prevent the error when you are dividing a number by zero.