Re: how to disable floating point exceptions/arithmetic exceptions?
Re: how to disable floating point exceptions/arithmetic exceptions?
- Subject: Re: how to disable floating point exceptions/arithmetic exceptions?
- From: Robert Purves <email@hidden>
- Date: Sat, 18 Aug 2007 12:00:24 +1200
Rua Haszard Morris wrote:
Is it possible to prevent an application crashing on EXC_ARITHMETIC
(e.g. divide by zero) on i386?
i = 1/0;
"Floating point exception"? I don't think so.
Your energies may be better spent tracking down integer divide-by-
zero.
That's a very valid point, I was ignoring that distinction!
I think the upshot is the same, there isn't a way to prevent
crashreporter noticing the mach exception on divide by zero...
Tut, tut, such pessimism...
Here's a complete working example that handles an exception caused by
integer divide by zero on x86.
$ cat exception_tester.c
#include <Carbon/Carbon.h>
#define BYTES_TO_SKIP 6
static OSStatus MyExceptionHandler( ExceptionInformation *
theException )
{
OSStatus err = -1;
switch ( theException->theKind )
{
case kIntegerException:
#if TARGET_CPU_X86
printf ( "Handling kIntegerException\n" );
theException->machineState->EIP +=
BYTES_TO_SKIP;
err = noErr; // we handled
#endif
break;
default:
break;
}
return err;
}
int main( void )
{
int i;
ExceptionHandler oldHandler;
printf( "Divide by zero coming...\n" );
oldHandler = InstallExceptionHandler( MyExceptionHandler );
i = 1/0;
(void) InstallExceptionHandler( oldHandler ); // restore
printf( "After divide by zero\n" );
return 0;
}
$ gcc exception_tester.c -framework Carbon && ./a.out
exception_tester.c: In function 'main':
exception_tester.c:31: warning: division by zero
Divide by zero coming...
Handling kIntegerException
After divide by zero
Of course you wouldn't use this technique in production code without
further study. The problem is BYTES_TO_SKIP, which is conveniently 4
for every instruction on PPC, but is variable on x86. The value 6 is
appropriate for idivl (a.k.a. idiv).
Robert P.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden