fexception handling in C

Discussion in 'C Programming' started by siliconwafer, Jul 27, 2005.

  1. siliconwafer

    siliconwafer Guest

    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
     
    siliconwafer, Jul 27, 2005
    #1
    1. Advertising

  2. siliconwafer

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Jul 27, 2005
    #2
    1. Advertising

  3. siliconwafer

    Free Bird Guest

    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.
     
    Free Bird, Jul 27, 2005
    #3
  4. siliconwafer

    John Bode Guest

    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.
     
    John Bode, Jul 27, 2005
    #4
  5. siliconwafer

    Lew Pitcher Guest

    -----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+5Xgb9Uga4wCg2MCC
    RyDC505EE6ZPiCROG9x6mSk=
    =tN02
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Jul 27, 2005
    #5
  6. siliconwafer

    Alan Balmer Guest

    On 27 Jul 2005 11:23:17 -0700, "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?


    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
     
    Alan Balmer, Jul 27, 2005
    #6
  7. siliconwafer

    Default User Guest

    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
     
    Default User, Jul 27, 2005
    #7
  8. siliconwafer

    siliconwafer Guest

    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
     
    siliconwafer, Jul 28, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.onet.pl

    session handling over HTTP

    news.onet.pl, Aug 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    599
    mbstevens
    Aug 12, 2005
  2. Robert V
    Replies:
    1
    Views:
    1,185
    Jeremy Fluhmann
    Jul 21, 2003
  3. Mark Tarver
    Replies:
    22
    Views:
    1,336
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,975
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    201
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page