fexception handling in C

S

siliconwafer

Hi All,
I am using C on linux and performed a 1/0 operation i.e a divide by
zero using integer variables/constants.it gives me a floating point
exceptionand the program terminates.
I want to access the routine that causes this message to occour and
want to modify it so that my program continues to run.How is exception
handling done in C?Is there a try-catch equivalent?
Any help apriciated.
Thanks,
Shekhar
 
E

Eric Sosman

siliconwafer said:
Hi All,
I am using C on linux and performed a 1/0 operation i.e a divide by
zero using integer variables/constants.it gives me a floating point
exceptionand the program terminates.
I want to access the routine that causes this message to occour and
want to modify it so that my program continues to run.How is exception
handling done in C?Is there a try-catch equivalent?

Interesting that you got a floating-point exception
from an integer calculation ... As far as C is concerned,
attempting to divide by zero produces "undefined behavior,"
meaning that anything at all can happen -- you can get a
floating-point exception, or a bogus answer without any kind
of error indication, or your CPU may overheat and melt into
a puddle of impure glass.

The C language itself makes no promises about what might
happen, but the particular system on which you're running C
might. If so, you'll need to check with people who know the
details of how your system operates; they may be able to tell
you some tricks that go beyond what C itself can deliver.

And no, C has no built-in exception handling, nor any
way to "repair and resume" a failed program. Again, your
system might provide some such capabilities as extensions
to C -- but again, you'll need to ask the system experts,
not the C experts.
 
F

Free Bird

siliconwafer said:
Hi All,
I am using C on linux and performed a 1/0 operation i.e a divide by
zero using integer variables/constants.it gives me a floating point
exceptionand the program terminates.
I want to access the routine that causes this message to occour and
want to modify it so that my program continues to run.How is exception
handling done in C?Is there a try-catch equivalent?

On Linux (but this is not guaranteed by the standard) a division by zero
will lead to a SIGFPE signal being raised. You can catch the signal in such
a way:

#include <signals.h>

void handler (int signum)
{
/* Signal handler */
}

int main (int argc, char *argv[])
{
signal (SIGFPE, handler);
/* Other code */
}

Keep in mind that your program will be in an undefined state after a
division by zero, so there's not much the signal handler can do except for
cleanup and exit. Don't even think about continuing your program's execution
if you want to write ISO C compatible code. C does not have anything like
C++'s exception handling mechanism, and you'd better just prevent divisions
by zero from occurring in the first place.
 
J

John Bode

siliconwafer said:
Hi All,
I am using C on linux and performed a 1/0 operation i.e a divide by
zero using integer variables/constants.it gives me a floating point
exceptionand the program terminates.
I want to access the routine that causes this message to occour and
want to modify it so that my program continues to run.How is exception
handling done in C?Is there a try-catch equivalent?
Any help apriciated.
Thanks,
Shekhar

Read up on the signal() function. It allows you to install an
interrupt handler to catch exceptional conditions and handle them as
you need to.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi All,
I am using C on linux and performed a 1/0 operation i.e a divide by
zero using integer variables/constants.it gives me a floating point
exceptionand the program terminates.
I want to access the routine that causes this message to occour and
want to modify it so that my program continues to run.How is exception
handling done in C?Is there a try-catch equivalent?

Try installing a signal() handler that intercepts SIGFPE signals

Be aware that, after the termination of the signal handler function that
intercepts SIGFPE signals, the behaviour the code is undefined by the C
standard.

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFC59gsagVFX4UWr64RAi7yAJ46rf6ECGj6W8FlorM+5Xgb9Uga4wCg2MCC
RyDC505EE6ZPiCROG9x6mSk=
=tN02
-----END PGP SIGNATURE-----
 
A

Alan Balmer

Hi All,
I am using C on linux and performed a 1/0 operation i.e a divide by
zero using integer variables/constants.it gives me a floating point
exceptionand the program terminates.
I want to access the routine that causes this message to occour and
want to modify it so that my program continues to run.How is exception
handling done in C?Is there a try-catch equivalent?

No. There is the signal() function, but the standard does not specify
any signal for division by zero. Your proper course of action is to
test the denominator for zero before doing the division.
 
D

Default User

siliconwafer said:
Hi All,
I am using C on linux and performed a 1/0 operation i.e a divide by
zero using integer variables/constants.it gives me a floating point
exceptionand the program terminates.
I want to access the routine that causes this message to occour and
want to modify it so that my program continues to run.How is exception
handling done in C?Is there a try-catch equivalent?
Any help apriciated.


Rather than try to handle the divide by 0, why not prevent it? You know
where it's happening, add logic to test the operands and handle the
situation.



Brian
 
S

siliconwafer

Default said:
Rather than try to handle the divide by 0, why not prevent it? You know
where it's happening, add logic to test the operands and handle the
situation.



Brian

Hi Again,
Thanks for all your suggestions.I tried using signal() function.
It takes 2 args,a constant difining the type of signal and a function
pointer to signal handler.Can one pass more than one args to signal
handler?The signal function is defined as
void (*signal (int sig, void (*func)(int)))(int);
That is to say, signal is a function that returns a pointer to another
function. This second function takes a single int argument and returns
void. The second argument to signal is similarly a pointer to a
function returning void which takes an int argument.
Now signal() also returns a pointer to a function.But to which
function?Is it any user defined function or any C/system related
function?If so how can one use this return value?
some more help appriciated
Thanks,
-siliconwafer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top