home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / full / delphi / DELPHI16 / TECHINFO / DELPHI / TIS / TI2814.FX < prev    next >
Encoding:
Text File  |  1995-08-24  |  6.1 KB  |  183 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2814
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  July 14, 1995                            PAGE  :  1/3
  11.  
  12.     TITLE  :  Handling EDBEngineError Exceptions
  13.  
  14.  
  15.  
  16.  
  17. Information that describes the conditions of a database engine error can
  18. be obtained for use by an application through the use of an EDBEngineError
  19. exception. EDBEngineError exceptions are handled in an application through
  20. the use of a try..except construct. When an EDBEngineError exception
  21. occurs, a EDBEngineError object would be created and various fields in that
  22. EDBEngineError object would be used to programmatically determine what
  23. went wrong and thus what needs to be done to correct the situation. Also,
  24. more than one error message may be generated for a given exception. This
  25. requires iterating through the multiple error messages to get needed info-
  26. rmation.
  27.  
  28. The fields that are most pertinent to this context are:
  29.  
  30.    ErrorCount: type Integer; indicates the number of errors that are in
  31.      the Errors property; counting begins at zero.
  32.  
  33.    Errors: type TDBError; a set of record-like structures that contain
  34.      information about each specific error generated; each record is
  35.      accessed via an index number of type Integer.
  36.  
  37.    Errors.ErrorCode: type DBIResult; indicating the BDE error code for the
  38.      error in the current Errors record.
  39.  
  40.    Errors.Category: type Byte; category of the error referenced by the
  41.      ErrorCode field.
  42.  
  43.    Errors.SubCode: type Byte; subcode for the value of ErrorCode.
  44.  
  45.    Errors.NativeError: type LongInt; remote error code returned from the
  46.      server; if zero, the error is not a server error; SQL statement
  47.      return codes appear in this field.
  48.  
  49.    Errors.Message: type TMessageStr; if the error is a server error, the
  50.      server message for the error in the current Errors record; if not a
  51.      server error, a BDE error message.
  52.  
  53. In a try..except construct, the EDBEngineError object is created directly
  54. in the except section of the construct. Once created, fields may be
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2814
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  July 14, 1995                            PAGE  :  2/3
  72.  
  73.     TITLE  :  Handling EDBEngineError Exceptions
  74.  
  75.  
  76.  
  77.  
  78. accessed normally, or the object may be passed to another procedure for
  79. inspection of the errors. Passing the EDBEngineError object to a special-
  80. ized procedure is preferred for an application to make the process more
  81. modular, reducing the amount of repeated code for parsing the object for
  82. error information. Alternately, a custom component could be created to
  83. serve this purpose, providing a set of functionality that is easily trans-
  84. ported across applications. The example below only demonstrates creating
  85. the DBEngineError object, passing it to a procedure, and parsing the
  86. object to extract error information.
  87.  
  88. In a try..except construct, the DBEngineError can be created with syntax
  89. such as that below:
  90.  
  91.   procedure TForm1.Button1Click(Sender: TObject);
  92.   var
  93.     i: Integer;
  94.   begin
  95.     if Edit1.Text > ' ' then begin
  96.       Table1.FieldByName('Number').AsInteger := StrToInt(Edit1.Text);
  97.       try
  98.         Table1.Post;
  99.       except on E: EDBEngineError do
  100.         ShowError(E);
  101.       end;
  102.     end;
  103.   end;
  104.  
  105. In this procedure, an attempt is made to change the value of a field in a
  106. table and then call the Post method of the corresponding TTable component.
  107. Only the attempt to post the change is being trapped in the try..except
  108. construct. If an EDBEngineError occurs, the except section of the con-
  109. struct is executed, which creates the EDBEngineError object (E) and then
  110. passes it to the procedure ShowError. Note that only an EDBEngineError
  111. exception is being accounted for in this construct. In a real-world sit-
  112. uation, this would likely be accompanied by checking for other types of
  113. exceptions.
  114.  
  115. The procedure ShowError takes the EDBEngineError, passed as a parameter,
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2814
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  July 14, 1995                            PAGE  :  3/3
  133.  
  134.     TITLE  :  Handling EDBEngineError Exceptions
  135.  
  136.  
  137.  
  138.  
  139. and queries the object for contained errors. In this example, information
  140. about the errors are displayed in a TMemo component. Alternately, the
  141. extracted values may never be displayed, but instead used as the basis for
  142. logic branching so the application can react to the errors. The first step
  143. in doing this is to establish the number of errors that actually occurred.
  144. This is the purpose of the ErrorCount property. This property supplies a
  145. value of type Integer that may be used to build a for loop to iterate
  146. through the errors contained in the object. Once the number of errors
  147. actually contained in the object is known, a loop can be used to visit
  148. each existing error (each represented by an Errors property record) and
  149. extract information about each error to be inserted into the TMemo comp-
  150. onent.
  151.  
  152.   procedure TForm1.ShowError(AExc: EDBEngineError);
  153.   var
  154.     i: Integer;
  155.   begin
  156.     Memo1.Lines.Clear;
  157.     Memo1.Lines.Add('Number of errors: ' + IntToStr(AExc.ErrorCount));
  158.     Memo1.Lines.Add('');
  159.     {Iterate through the Errors records}
  160.     for i := 0 to AExc.ErrorCount - 1 do begin
  161.       Memo1.Lines.Add('Message: ' + AExc.Errors[i].Message);
  162.       Memo1.Lines.Add('   Category: ' + 
  163.         IntToStr(AExc.Errors[i].Category));
  164.       Memo1.Lines.Add('   Error Code: ' + 
  165.         IntToStr(AExc.Errors[i].ErrorCode));
  166.       Memo1.Lines.Add('   SubCode: ' + 
  167.         IntToStr(AExc.Errors[i].SubCode));
  168.       Memo1.Lines.Add('   Native Error: ' + 
  169.         IntToStr(AExc.Errors[i].NativeError));
  170.       Memo1.Lines.Add('');
  171.     end;
  172.   end;
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. DISCLAIMER: You have the right to use this technical information
  180. subject to the terms of the No-Nonsense License Statement that
  181. you received with the Borland product to which this information
  182. pertains.
  183.