473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

fexception handling in C

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

Nov 15 '05 #1
7 1987


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.

--
Er*********@sun.com

Nov 15 '05 #2
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.
Nov 15 '05 #3


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.

Nov 15 '05 #4
-----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-----
Nov 15 '05 #5
On 27 Jul 2005 11:23:17 -0700, "siliconwafer" <sp*********@yahoo.com>
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
re************************@att.net
Nov 15 '05 #6
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
Nov 15 '05 #7


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

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that...
7
by: yogeshnelwadkar | last post by:
Hello, i have a problem with replacing c++ exception handling with structured exception handling. How to replace the " catch(...) " in c++ exception handling with, __except , a structured...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.