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?
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.
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,
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.
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;
}