Thursday, November 20, 2008

Exception Handling in C programming language


Consider the following program:
#include
void main( )
{
float i ;
i = pow ( -2, 3 ) ;
printf ( "%f", i ) ;
}
int matherr ( struct exception *a )
{
if ( a -> type == DOMAIN )
{
if ( !strcmp ( a -> name, "pow" ) )
{
a -> retval = pow ( - ( a -> arg1 ), a -> arg2 ) ;
return 1 ;
}
}
return 0 ;
}

If we pass a negative value in pow( ) function a run time error occurs. If we wish to get the proper output even after passing a negative value in the pow( ) function we must handle the run time error. For this, we can define a function matherr( ) which is declared in the 'math.h' file. In this function we can detect the run-time error and write our code to correct the error. The elements of the exception structure receives the function name and arguments of the function causing the exception.


No comments:

Blog List