C - SPLessons

Chapter 33

C Error Handling

C Error Handling

Error is something which disturbs the flow of execution of a program. Handling these errors is called as C Error Handling.
 

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:

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:

Ignores the errors

Ignoring the errors is unprofessional which may lead to many other errors, if not handled at proper time.

Terminating the program

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 stdlib directory.

#include<stdio.h>
#include<conio.h>
#include<math.h>
{
	int a=12;
	int b=0;
	c=a/b;
	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.

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void mistake(int signal) 
{
	puts("signal caught");
}
int main(void)
{
	if (signal(x,y) == sig_err) 
	{
		fputs("Error occurred when setting a signal handler\n", stderr);
		return EXIT_FAILURE;
	}
	puts("Raising the signal.");
	if (raise(x) != 0)
	{
		fputs("Error raising the signal\n", stderr);
		return EXIT_FAILURE;
	}
	puts("exit");
	return 0;
}

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.

#include <stdio.h> 
#include <stdlib.h>
int main()
{
	int a= 12;
	int b= 0;
	int c;
	if (b == 0) // calls stderr
	{
		fprintf("cannot divide by zero\n",stderr);
		exit(EXIT_FAILURE);
	}
	c=a/b;
	exit(EXIT_SUCCESS);
}

setjmp() or longjmp()

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.

#include<stdio.h>
#include<setjmp.h>
#include<stdlib.h>
int main()
{
	int a=10;
	int b;
	int c;
	jmp_buf env;
	printf("Enter the dividend value b\n");
	setjmp(env);// This stores the value of scanf() in buffer
	scanf(%f",&b);

	if(b==0)
	longjmp(env,2); // goes to where we stopped
	c=(a/b);
	printf(%.2f\n", c);
	exit(EXIT_SUCCESS);
	return 0;
}

Summary

  • 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.