Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fexception handling in C

Reply
Thread Tools

fexception handling in C

 
 
siliconwafer
Guest
Posts: n/a
 
      07-27-2005
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-27-2005


siliconwafer wrote:
> 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Free Bird
Guest
Posts: n/a
 
      07-27-2005
siliconwafer wrote:

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


 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      07-27-2005


siliconwafer wrote:
> 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.

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      07-27-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

siliconwafer wrote:
> 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+5X gb9Uga4wCg2MCC
RyDC505EE6ZPiCROG9x6mSk=
=tN02
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      07-27-2005
On 27 Jul 2005 11:23:17 -0700, "siliconwafer" <(E-Mail Removed)>
wrote:

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

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      07-27-2005
siliconwafer wrote:

> 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
 
Reply With Quote
 
siliconwafer
Guest
Posts: n/a
 
      07-28-2005


Default User wrote:
> siliconwafer wrote:
>
> > 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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
python list handling and Lisp list handling Mark Tarver Python 22 04-26-2009 09:36 PM
Is faster handling hexadecimal values than handling chars? IƱaki Baz Castillo Ruby 1 04-15-2008 09:04 AM
cookie handling - accept from domain or specific sub-domains ?? cookie-monster Firefox 2 03-07-2005 01:13 PM
Thunderbirds handling of jpgs and gif attached to email Dan Firefox 6 10-05-2004 01:25 PM



Advertisments