Functions for temporarily disabling exceptions


The following functionality requires you to select a floating-point model that supports exceptions, such as --fpmode=ieee_full or --fpmode=ieee_fixed.

C99 provides two functions that enable you to avoid risking exception traps when executing code that might cause exceptions. This is useful when, for example, trapped exceptions are using the ARM default behavior. The default is to cause SIGFPE and terminate the application.

int feholdexcept(fenv_t *envp); void feupdateenv(const fenv_t *envp);

The feholdexcept() function saves the current floating-point environment in the fenv_t variable provided, sets all exceptions to be untrapped, and clears all the exception sticky flags. You can then execute code that might cause unwanted exceptions, and make sure the sticky flags for those exceptions are cleared. Then you can call feupdateenv(). This restores any exception traps and calls them if necessary. For example, suppose you have a function, frob(), that might cause the Underflow or Invalid Operation exceptions (assuming both exceptions are trapped). You are not interested in Underflow, but you want to know if an invalid operation is attempted. You can implement the following code to do this:

fenv_t env;

Then, if the frob() function raises Underflow, it is cleared again by feclearexcept(), so no trap occurs when feupdateenv() is called. However, if frob() raises Invalid Operation, the sticky flag is set when feupdateenv() is called, so the trap handler is invoked.

This mechanism is provided by C99 because C99 specifies no way to change exception trapping for individual exceptions. A better method is to use __ieee_status() to disable the Underflow trap while leaving the Invalid Operation trap enabled. This has the advantage that the Invalid Operation trap handler is provided with all the information about the invalid operation (that is, what operation was being performed, and on what data), and can invent a result for the operation. Using the C99 method, the Invalid Operation trap handler is called after the fact, receives no information about the cause of the exception, and is called too late to provide a substitute result.

Show/hideSee also


ARM® C and C++ Libraries and Floating-Point Support Reference:

Compiler Reference:

Copyright © 2010-2011 ARM. All rights reserved.ARM DUI 0475C