<< Click to Display Table of Contents >> Navigation: Apollo VCL Components > Apollo VCL Component Reference > TApolloEnv > TApolloEnv Methods > SetErrorFunc |
Declaration
function SetErrorFunc( const pFunc, pData : LongInt ): LongInt;
Description
Allows defining a function to be called in place of Apollo's error message when an Apollo error is raised. To completely disable Apollo's default error-handling system, you should also place a single call to TApolloEnv.ErrorLevel(0) at the beginning of the application.
This method is not supported with remote table access using Apollo Database Server. Remote errors will be passed through to the TApolloConnection.OnError event. See the Delphi-compatible ErrFunc sample project under the ..\VCL\Samples\Delphi\ErrFunc directory for details.
Parameters
pFunc : Pointer to function to be called when an Apollo error is generated. Pass zero (0) to pFunc to unhook the function and return to Apollo's default error messaging system.
pData : Pointer to UserError record structure (see example below for record definition).
Returns
A pointer to the previously-defined function to be called, if any, is returned.
Delphi Example
// ----------------------------------------------------
// The complete sample project can be found in
// ..\VCL\Samples\Delphi\ErrFunc directory
// ----------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Grids, DBGrids, Db, DBTables, StdCtrls, ApoDSet;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
ListBox1: TListBox;
Button1: TButton;
Label1: TLabel;
ApTbl: TApolloTable;
ApolloEnv1: TApolloEnv;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Type
pUserError = ^UserError;
UserError = record
iError : Integer;
iWorkArea : Integer;
ErrorMsg : LongInt;
ExtraMsg : LongInt;
userDefined: LongInt;
end;
procedure MyErrorProc( info : pUserError ); stdcall;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
EInfo : UserError;
// MyErrorProc must be a stand-alone procedure, not a method.
// Eg. Do NOT preface the function with "TForm1." or otherwise...
procedure MyErrorProc( info : pUserError );
const
CRLF = Chr(13) + Chr(10);
var
listBox : TListBox;
theInfo : UserError;
begin
theInfo := pUserError(info)^;
// These lines add the error message info into lines of ListBox1
listBox := TListBox( theInfo.UserDefined );
listbox.Items.Add( 'Error #: ' + IntToStr( theInfo.iError));
listbox.Items.Add( PChar(theInfo.ErrorMsg) );
listbox.Items.Add( PChar(theInfo.ExtraMsg) );
// Or a simpler use just adds the info to a standard message box
ShowMessage( 'Error #: ' + IntToStr(theInfo.iError) + CRLF +
PChar( theInfo.ErrorMsg ) + ' ' +
PChar( theInfo.ExtraMsg ));
end;
// Set up ErrorFunc hook at program startup
procedure TForm1.FormCreate(Sender: TObject);
begin
Einfo.iError := LongInt(0);
Einfo.ErrorMsg := LongInt(0);
Einfo.ExtraMsg := LongInt(0);
EInfo.userDefined := LongInt(ListBox1);
ApolloEnv1.ErrorLevel(2);
ApolloEnv1.SetErrorFunc( LongInt(@MyErrorProc), LongInt(@Einfo) );
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// The TRY...EXCEPT BLOCK avoids the standard "File does not exist"
// Message Box. Be SURE to uncheck "Stop on Delphi Exceptions" in
// the Delphi environment when testing this from within the Delphi IDE.
try
if ApTbl.Active then
ApTbl.Close
else
begin
ApTbl.DatabaseName := ExtractFilePath( Application.ExeName );
ApTbl.TableName := 'FooBar.dbf';
ApTbl.Open;
end;
except
// silent exception
end;
end;
end.
See Also
ErrorLevel, Error Handling