Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ User's Guide - we would all like to create flawless software but situations arise for which http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
We would all like to create flawless software but situations arise for which
we did not plan.  An event that we did not expect which causes the software
to cease to function properly is called an exception.  The computer can
generate a hardware exception when the software attempts to execute an
illegal instruction.  We can force this quite easily in C by dereferencing a
NULL pointer as shown in the following sample fragment of code.

Example:

         char *nullp = NULL;

         *nullp = '\1';

We can also generate software exceptions from software by calling a special
function for this purpose.  We will look at software exceptions in more
detail later on.

Given that exceptions are generally very difficult to avoid in large
software projects, we can acknowledge that they are a fact of life and
prepare for them.  A mechanism similar to try/finally has been devised that
makes it possible to gain control when an exception occurs and to execute
procedures to handle the situation.

The exception handling mechanism involves the pairing up of a _try block
with an _except block.  This is illustrated in the following example.

Example:

     #include <stdio.h>
     #include <stdlib.h>
     #include <excpt.h>

     void main( int argc, char **argv )
     {
       char *nullp = NULL;


       printf( "Attempting illegal memory reference.\n" );
       _try {
         *nullp = '\1';
       }
       _except (EXCEPTION_EXECUTE_HANDLER) {
         printf( "Oh no! We had an exception!\n" );
       }
       printf( "We recovered fine...\n" );
     }

In this example, any exception that occurs while executing "inside" the try
block will cause the except block to execute.  Unlike the finally block,
execution of the except block occurs only when an exception is generated and
only when the expression after the _except keyword evaluates to
 EXCEPTION_EXECUTE_HANDLER.  The expression can be quite complex and can
involve the execution of a function that returns one of the permissible
values.  The expression is called the exception "filter" since it determines
whether or not the exception is to be handled by the except block.  The
permissible result values for the exception filer are:

EXCEPTION_EXECUTE_HANDLER
    meaning "I will handle the exception".

EXCEPTION_CONTINUE_EXECUTION
    meaning "I want to resume execution at the point where the exception was
    generated".

EXCEPTION_CONTINUE_SEARCH
    meaning "I do not want to handle the exception so continue looking down
    the try/except chain until you find an exception handler that does want
    to handle the exception".

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson