home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / desk / examples / Desk / Examples / Error / c / Main
Encoding:
Text File  |  1997-05-20  |  1.6 KB  |  91 lines

  1. #include <stdio.h>
  2.  
  3. #include "Desk.Error2.h"
  4. #include "Desk.DeskMem.h"
  5. #include "Desk.Debug.h"
  6.  
  7.  
  8. static void    Foo( void)
  9.     {
  10.     Desk_Error2_TryCatch(
  11.         Desk_Error2_HandleText( Desk_error_PLACE "Example text error");
  12.         ,
  13.         Desk_Error2_Describe( stdout, &Desk_Error2_globalblock);
  14.         printf( "\n");
  15.         )
  16.     return;
  17.     }
  18.  
  19.  
  20. static void    Bar( void)
  21. /*
  22. This is an example of how to clean up after errors...
  23.  */
  24.     {
  25.     void* volatile    p1 = NULL;
  26.     void* volatile    p2 = NULL;
  27.     /* 
  28.     These are volatile because non-volatile automatic 
  29.     variables have undefined values after a longjmp.
  30.      */
  31.     
  32.     Desk_Error2_Try    {
  33.         p1 = Desk_DeskMem_Malloc( 32);
  34.         p2 = Desk_DeskMem_Malloc( 99999999);
  35.         /*
  36.         Use p1 and p2, call any functions 
  37.         which could raise an error etc
  38.          */
  39.         }
  40.     Desk_Error2_Catch    {
  41.         /*
  42.         If we get here, an error has occurred 
  43.         in the 'Try' block above.
  44.          */
  45.         /*
  46.         If p1 and p2 weren't volatile, they 
  47.         would have undefined values here.
  48.          */
  49.         Desk_DeskMem_Free( p1);
  50.         Desk_DeskMem_Free( p2);
  51.         Desk_Error2_ReThrow();
  52.         }
  53.     Desk_Error2_EndCatch
  54.     
  55.     Desk_DeskMem_Free( p1);
  56.     Desk_DeskMem_Free( p2);
  57.     }
  58.  
  59.  
  60.  
  61.  
  62. int    main( void)
  63. {
  64. Desk_Error2_Init_JumpSig();
  65. Desk_Debug_SetNestingIndentation( "  ");
  66. Desk_Debug_Printf( "Started...\n");
  67.  
  68. Desk_Error2_Try    {
  69.     Foo();
  70.     Desk_DeskMem_Malloc( 100000000);    /* Should cause an error...    */
  71.     }
  72. Desk_Error2_Catch    {
  73.     printf( Desk_error_PLACE "An error occurred: ");
  74.     Desk_Error2_Describe( stdout, &Desk_Error2_globalblock);
  75.     printf( "\n");
  76.     }
  77. Desk_Error2_EndCatch
  78.  
  79. Desk_Error2_TryCatch(
  80.     Bar();
  81.     ,
  82.     printf( Desk_error_PLACE "An error occurred from Bar(): ");
  83.     Desk_Error2_Describe( stdout, &Desk_Error2_globalblock);
  84.     printf( "\n");
  85.     )
  86.  
  87. printf( "Hello world\n");
  88.  
  89. return 0;
  90. }
  91.