ERRORS AND EXCEPTIONS
99
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
and
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
or
100
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.
Addit
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.
User-Generated Exceptions
It is possible to raise an exception at any point during program execution using
the
function. In this way, your program can detect an error and
treat it as an exception.
Parameters
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
code hex
digit is shown with bit 29 set.
0—Success (lead exception code hex digit is 2).
•1Informational (lead exception code hex digit is 6).
•2Warning (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
generate
; doing so will cause an immediate
exception.
, if not , points to an array of size (the third
parameter) containing 32-bit values to be passed to the filter expression. There is

Get Windows System Programming Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.