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 - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

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

     void main( int argc, char **argv )
     {
       read_file( fopen( argv[1], "r" ) );
     }


     void read_file( FILE *input )
     {
       int         line = 0;
       char        buffer[256];
       char        icode;
       char        x, y;

       if( input == NULL ) {
         printf( "Unable to open file\n" );
         return;
       }


       _try {
         for(;;) {
           line++;
           if( fgets( buffer, 255, input ) == NULL ) break;
           icode = buffer[0];
           if( icode != '1' ) _leave;
           x = buffer[1];
           line++;
           if( fgets( buffer, 255, input ) == NULL ) _leave;
           icode = buffer[0];
           if( icode != '2' ) _leave;
           y = buffer[1];
           process( x, y );
         }
         printf( "Processing complete\n" );
         fclose( input );
         input = NULL;
       }


       _finally {
         if( input != NULL ) {
           printf( "Invalid sequence: line = %d\n", line );
           fclose( input );
         }
       }
     }


     void process( char x, char y )
     {
       printf( "processing pair %c,%c\n", x, y );
     }

There are two ways to enter the finally block.  One way is caused by unwinds
- either local (by the use of break,  continue,  return, or goto ) or global
(more on global unwinds later).  The other way is through the normal flow of
execution (i.e., simply by falling through the bottom of the try block).
 There is a function called AbnormalTermination that can be used to
determine which of these two methods was used to enter the finally block.
 If the function returns TRUE (1) then the finally block was entered using
the first method; if the function returns FALSE (0) then the finally block
was entered using the second method.  This information may be useful in some
circumstances.  For example, you may wish to avoid executing any code in a
finally block if the block was entered through the normal flow of execution.

Example:

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

     void main( int argc, char **argv )
     {
       read_file( fopen( argv[1], "r" ) );
     }


     void read_file( FILE *input )
     {
       int         line = 0;
       char        buffer[256];
       char        icode;
       char        x, y;

       if( input == NULL ) {
         printf( "Unable to open file\n" );
         return;
       }


       _try {
         for(;;) {
           line++;
           if( fgets( buffer, 255, input ) == NULL ) break;
           icode = buffer[0];
           if( icode != '1' ) return;
           x = buffer[1];
           line++;
           if( fgets( buffer, 255, input ) == NULL ) return;
           icode = buffer[0];
           if( icode != '2' ) return;
           y = buffer[1];
           process( x, y );
         }
         printf( "Processing complete\n" );
       }


       _finally {
         if( AbnormalTermination() )
             printf( "Invalid sequence: line = %d\n", line );
         fclose( input );
       }
     }


     void process( char x, char y )
     {
       printf( "processing pair %c,%c\n", x, y );
     }

In the above example, we reverted back to the use of the return statement
since the execution of a _leave statement is considered part of the normal
flow of execution and is not considered an "abnormal termination" of the try
block.  Note that since it is not possible to determine whether the finally
block is executing as the result of a local or global unwind, it may not be
appropriate to use the AbnormalTermination function as a way to determine
what has gone on.  However, in our simple example, we expect that nothing
could go wrong in the "processing" routine.

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