ERRORS AND EXCEPTIONS
This example enables all possible floating-point exceptions except for the
floating-point stack overflow,
. Alternativ ely,
enable specific exceptions by using only selected exception masks, such as
. Program 4–3 uses similar code in the context of a larger example.
Errors and Exceptions
An error can be thought of as a situation that could occur occasionally in known
locations. System call errors, for example, should be detected and reported imme-
diately by logic in the code. Thus, programmers normally include an explicit test
to see, for i nstance, whether a file read operation ha
s fai led. The
function was developed in Chapter 2 to diagnose and respond to errors.
An exception, on the other hand, could occur nearly anywhere, and it is not
possible or practical to test for an exception. Division by zero and memory access
violations are examples.
Nonetheless, the distinction is sometimes blurre
d. Windows will, optionally,
generate exceptions during memory allocation using the
functions if memory is insufficient. This is described in Chapter 5 .
Programs can also raise their own exceptions with programmer-defined exception
cod es using the
function, as described next.
Exception handlers provide a convenient mechanism for exiting from inner
blocks or functions under program control without resorting to a
CHAPTER 4 EXCEPTION HANDLING
to transfer control. This capability is particularly important if the block has
accessed resources, such as open files, memory, or synchronization objects,
because the handler can release them. It is also possible to continue program
execution after the exception handler, rather than terminate the program.
ionally, a program can restore system state, such as the floating-point mask,
on exiting from a block. Many examples use handlers in this way.
It is possible to raise an exception at any point during program execution using
function. In this way, your program can detect an error and
treat it as an exception.
is the user-defined code. Do not use bit 28, which is reserved
for the system. The error code is encoded in bits 27–0 (all except the most signifi-
cant hex digit). Bit 29 should be set to indicate a “customer” (not Microsoft) excep-
tion. Bits 31–30 encode the severity as follows, where the resulting lead exception
digit is shown with bit 29 set.
• 0—Success (lead exception code hex digit is 2).
•1—Informational (lead exception code hex digit is 6).
•2—Warning (lead exception code hex digit is A).
• 3—Error (lead exception code hex digit is E).
is normally set to , but setting the value to
indicates that the filter expression should not
; doing so will cause an immediate
, if not , points to an array of size (the third
parameter) containing 32-bit values to be passed to the filter expression. There is