home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / ERRVAL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  3.7 KB  |  98 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // errval.c
  4. #include "snipit.h"
  5.  
  6. //=====================================================================
  7. //  Function:
  8. //          ErrVal();
  9. //
  10. //  Description:
  11. //          IDAPI functions return error codes to inform the calling
  12. //          program if the function succeeded or failed. The return value
  13. //          will be equal to DBIERR_NONE when the function was
  14. //          successful.
  15. //          This example shows how to get information about an error
  16. //          when an IDAPI function returns a value other than DBIERR_NONE.
  17. //=====================================================================
  18. void
  19. ErrVal (void)
  20. {
  21.     DBIResult   rslt;       // Value returned from IDAPI functions
  22.     hDBIDb      hDb;        // Handle to the database
  23.     DBIMSG      dbi_status; // Error String
  24.     DBIErrInfo  ErrInfo;    // Contains information about the error
  25.  
  26.     Screen("*** Getting Error Information ***\r\n");
  27.  
  28.     BREAK_IN_DEBUGGER();
  29.  
  30.     // Open a standard database (used to access Paradox and dBASE
  31.     //   tables), by using a NULL database type. Note that this will fail
  32.     //   because we have not initialized IDAPI.
  33.     Screen("    Attempt to open the database...");
  34.     Screen("        Error Expected - Engine not initialized.");
  35.     rslt = DbiOpenDatabase("", NULL, dbiREADWRITE, dbiOPENSHARED,
  36.                            NULL, 0, NULL, NULL, &hDb);
  37.     if ((rslt != DBIERR_CANTFINDODAPI) && (rslt != DBIERR_NOTINITIALIZED))
  38.     {
  39.         if (rslt != DBIERR_NONE)
  40.         {
  41.             // Note - make certain to call DbiGetErrorInfo() right after
  42.             //   the error as it will only give information about the most
  43.             //   recent error.
  44.             DbiGetErrorInfo(TRUE, &ErrInfo);
  45.  
  46.             // Check to see if DbiGetErrorInfo is reporting information
  47.             //   on the same error that you have. (Done in case DbiGetErrorInfo
  48.             //   was not called immediately after the function which failed.)
  49.             if (ErrInfo.iError == rslt)
  50.             {
  51.                 Screen("        ERROR -  Cat:Code = [%xh:%xh]"
  52.                        "\r\n                 %s",
  53.                        ErrCat(ErrInfo.iError), ErrCode(ErrInfo.iError),
  54.                        ErrInfo.szErrCode);
  55.  
  56.                 // Need to check how much information was provided -
  57.                 //   different errors return different amounts of information.
  58.                 if (strcmp(ErrInfo.szContext1, ""))
  59.                 {
  60.                     Screen("                 %s", ErrInfo.szContext1);
  61.                 }
  62.                 if (strcmp(ErrInfo.szContext2, ""))
  63.                 {
  64.                     Screen("                 %s", ErrInfo.szContext2);
  65.                 }
  66.                 if (strcmp(ErrInfo.szContext3, ""))
  67.                 {
  68.                     Screen("                 %s", ErrInfo.szContext3);
  69.                 }
  70.                 if (strcmp(ErrInfo.szContext4, ""))
  71.                 {
  72.                     Screen("                 %s", ErrInfo.szContext4);
  73.                 }
  74.             }
  75.             else
  76.             {
  77.                 DbiGetErrorString(rslt, dbi_status);
  78.                 Screen("        ERROR -  Cat:Code [%xh:%xh]"
  79.                        "\r\n                 %s", ErrCat(rslt), ErrCode(rslt),
  80.                        dbi_status);
  81.             }
  82.         }
  83.     }
  84.     else
  85.     {
  86.         Screen("        Engine not initialized.");
  87.     }
  88.  
  89.     // Check if this is the expected error.
  90.     if (rslt != DBIERR_NOTINITIALIZED)
  91.     {
  92.         Screen("        Unexpected Condition - terminating example");
  93.     }
  94.  
  95.     Screen("\r\n*** End of Example ***");
  96. }
  97.  
  98.