Error Handling
Error Handling
• Although C does not provide direct support to error handling (or exception handling), there
• A programmer has to prevent errors in the first place and test return values from the
functions.
• A lot of C function calls return -1 or NULL or set an in case of an error code as the global
variable errno, so quick tests on these values are easily done with an instance of ‘if
statement’.
What is errno?
errno is a global variable indicating the error occurred during any function call and it is
(value) which can be used to identify the type of error that has been encountered.
int main()
{
// If a file is opened which does not exist,
// then it will be an error and corresponding
// errno value will be set
FILE* fp;
return 0;
}
OutputValue of errno: 2
Note: Here the errno is set to 2 which means “No such file or directory”. It may give Errno 13 in online IDE, which says permission
denied.
Different Methods for Error Handling in C
Different methods are used to handle different kinds of errors in C. Some of the
commonly used methods are:
1.perror()
2.strerr()
3.ferror()
4.feof()
5.clearerr()
6.Exit Status
7.Divide by Zero Error
1. perror()
The perror() function is used to show the error description. It displays the string you pass to it,
followed by a colon, a space, and then the textual representation of the current errno value.
Syntax
int main()
{
FILE* fp;
The strerror() function is also used to show the error description. This function returns a pointer to
Syntax
Parameters
int main()
{
FILE* fp;
return 0;
}
OutputValue of errno: 2 The error message is : No such file or directory
3. ferror()
The ferror() function is used to check whether an error occurred during a file
operation.
Syntax
int ferror(FILE *stream);
Parameters
•stream: It is the pointer that points to the FILE for which we want to check the
error.
Return Value
•It returns a non-zero value if an error occurred, otherwise it returns 0.
// C program to demonstrate the ferror() function while ((c = fgetc(file)) != EOF) {
#include <stdio.h> }
void main() {
float a = 10;
float b = 5;
if (b = 0) { // we wrote = instead of ==
printf("Division by zero is not possible");
} else {
printf("The output is: %f", a/b);
}
}
Output:
INF signifies a division by zero error. In the above example, at line 8, we wanted to check whether the variable
b was equal to zero. But instead of using the equal to comparison operator (==), we use the assignment
operator (=). Because of this, the if statement became false and the value of b became 0. Finally, the else
clause got executed.
4. Semantic Error
Errors that occur because the compiler is unable to understand the written code are called Semantic Errors. A
semantic error will be generated if the code makes no sense to the compiler, even though it is syntactically
correct. It is like using the wrong word in the wrong place in the English language. For example, adding a
string to an integer will generate a semantic error.
Semantic errors are different from syntax errors, as syntax errors signify that the structure of a program is
incorrect without considering its meaning. On the other hand, semantic errors signify the incorrect
implementation of a program by considering the meaning of the program.
The most commonly occurring semantic errors are: use of un-initialized variables, type compatibility, and
array index out of bounds.
Example 1:
#include <stdio.h>
void main() {
int a, b, c;
a * b = c;
// This will generate a semantic error
}
Output:
Example 2:
#include <stdio.h>
In the above example, we printed six elements while the array arr only had five. Because we tried to access the sixth
element of the array, we got a semantic error and hence, the program generated a garbage value.
5. Linker Error
Linker is a program that takes the object files generated by the compiler and combines them into a
single executable file. Linker errors are the errors encountered when the executable file of the code
can not be generated even though the code gets compiled successfully. This error is generated when
a different object file is unable to link with the main object file. We can run into a linked error if we
have imported an incorrect header file in the code, we have a wrong function declaration, etc.