4

The following catch() is not called:

void test(void)
{
    int i=1,j=0,k;
    try
    {
        k = i/j;
    }
    catch(...)
    {
        ...handle it...
    }
}

Is there a way to catch this kind of exception?

tehvan
  • 10,189
  • 5
  • 27
  • 31
slashmais
  • 7,069
  • 9
  • 54
  • 80

4 Answers4

9

Please check http://linux.die.net/man/1/gcc there is a compiler option -mcheck-zero-division to handle this.

Alternatively, installing a SIGFPE handler might be an option, A float div by 0 would then generate a 'FPE_ZERODIVIDE'

         signal(SIGFPE, (fptr) FPE_ExceptionHandler);

         void FPE_ExceptionHandler(int nSig,int nErrType,int */*pnReglist*/)
          {
                switch(nErrType)
                  {
                    case FPE_ZERODIVIDE:  /* ??? */ break;
                }

            }

since

Most floating point systems are based on the IEEE standard, which allows division by 0. This returns either positive infinity or negative infinity as appropriate based on the signs of the numbers. (Except 0/0 returns the undefined NAN--again not an exceptional case.) This tends to be useful for scientific and mathematical applications. The NANs effectively signal a case where calculations were not pssible but allow calculations to continue. The continued calculations will not produce new results but will continue to return NANs. This allows long long chains of calculations to be performed witout error checking within the calculatiosn. The error checks only need to be performed at the very end of the work. This makes the code much much simpler and also faster. It can also be more useful at times as for some applications, infintity is a "useful" result, not really a sign of problems.

lakshmanaraj
  • 4,145
  • 23
  • 12
  • 1
    Note that the option -mcheck-zero-division is specific to the MIPS architecture. E.g. it won't work on x86. – janneb Mar 10 '09 at 22:36
3

No - there is no exception thrown (you get a signal - probably SIGFPE). You need to check for possible divide by zeros in your code, and then throw an exception yourself,

3

If this causes a run-time error at all (see lakshmanaraj's nice discussion on IEEE maths, though some compiler will let you force errors instead of NaNs), it throws a floating point exception signal.

Signals are a different mechanism than c++ exceptions, and are handled at the OS level. There are already a number of SO questions concerning the *nix signal mechanism, including:

For windows, you'll have to ask someone else. Mac OS X is--of course-- a unix derived system.

Community
  • 1
  • 1
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • Windows has its own mechanism called Structured Exception Handling, You'll see it abbreviated as SEH: http://msdn.microsoft.com/en-us/library/ms680657(VS.85).aspx – Jared Oberhaus Mar 06 '09 at 21:54
1

below code implement __try/__except effect like in visual studio c++ or how to simulate __try/__except for gcc or g++

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>

__thread jmp_buf * gThreadData; //thread local storage variable declare

void FPE_ExceptionHandler(int signal)
{
    printf("exception handler signalid=%d\n", signal);

    //jmp to setjmp_return and rc will equal to non zero
    longjmp(*gThreadData, 10001);
}

int main(int argc, char *argv[])
{
    //setup a callback function for access violation exception
    signal(SIGSEGV, (__sighandler_t)FPE_ExceptionHandler);

    //allocate a jmp_buf struct and assign it to thread local storage pointer
    gThreadData = (jmp_buf *)(new jmp_buf);

    //setjmp save current thread context
    int rc = setjmp(*gThreadData);

    //setjmp_return
    //first time, run to here rc will equal to 0
    if (rc == 0) {
        *(int*)0 = 1; //generate a exception
    }

    printf("return from exception\n");
    delete (jmp_buf *)gThreadData;
}
Darwin Zou
  • 26
  • 4
  • Hey and welcome! Your answer is a little hard to understand, and the script you shared looks quite complex for the thing that OP's asking. Perhaps you can explain the code you shared a little bit better? – Zimano Jun 20 '22 at 18:32
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 20 '22 at 18:32
  • Interesting approach, will give it a try-out. A way to be aware of the error at re-entry is to maintain some kind of state-variable/struct, possibly as a global, in order to do error-handling. (PS. to error-correct the lazy pedants in the other comments, maybe make your example compile-able by adding the needed headers - for me your answer is clear and to the point - thanks) – slashmais Jun 26 '22 at 06:23
  • Hi slashmais, you are right, I will add including code to enable it compile-able. – Darwin Zou Jun 27 '22 at 12:34