Error is something which disturbs the flow of execution of a program. Handling these errors is called as C Error Handling.
Basic type of errors
- Semantic error: occurs due to improper use of program statements.
- logical error: occurs due to mistakes in logical declarations.
- Syntax error: occurs when syntax is not properly specified.
The above errors can be detected and handled by the compiler itself.
There are few errors which cannot be detected by the compiler. They are:
- Run-time errors.
- Compile time errors.
As these errors are not identified by the compiler, they are termed as "Exceptions
". Exceptions occurs during the time of execution of the program.
- Errors occur at "run time" and "compile time" but, Exceptions occurs mostly at "run-time".
- Programmer itself should detect the exception. Errors can be handled by the compiler.
- C Error Handling is easier than exception handling.
Basically C does not provide direct support to error handling. But, it gives solution to the errors in various ways:
Ignoring the errors is unprofessional which may lead to many other errors, if not handled at proper time.
Terminating the program is really worst when compared to the above one. When there occurs an unavoidable error, which cannot be recovered again, then the program gets terminated indirectly leading to loss of data.
For example, when any number divided by zero results in infinity, which is not taken by the compiler. In that case, the program gets terminated. exit() function is specified in
printf(" The program is terminating");
Setting the error signals
When an error indicator is set in the program, compiler raises a signal whenever an error occurs disturbing the normal flow of program execution.
To handle all of these signals, we need to define "signal.h" as the header file. When an error occurs in the execution, a signal() function is called and checks in 'signal.h' for handling the respective error.
void mistake(int signal)
if (signal(x,y) == sig_err)
fputs("Error occurred when setting a signal handler\n", stderr);
puts("Raising the signal.");
if (raise(x) != 0)
fputs("Error raising the signal\n", stderr);
Showing error message and call exit() function
As seen in second way, the program gets terminated without any prior intimation. Here, that problem overcomes by giving an error message and then exits finally.
int a= 12;
int b= 0;
if (b == 0) // calls stderr
fprintf("cannot divide by zero\n",stderr);
setjmp() and longjmp() functions can be used to move away from a location into other function until the error gets rectified.
- Whenever a error occurs,setjmp() is called.
- Till then the recent work done can be stored in buffer called "jmp buf". If checking of exception completes, setjmp() returns zero to longjmp().
- Immediately execution carries on from the point where it was stopped earlier.
printf("Enter the dividend value b\n");
setjmp(env);// This stores the value of scanf() in buffer
longjmp(env,2); // goes to where we stopped
- Errors stops the flow of program.
- One can set error signals or call exit function or use setjmp(), longjmp() functions to handle errors.
setjmp() function leads to loss of code.