Name

RunError Procedure

Syntax

procedure RunError(ErrorCode: Integer = 0);

Description

The RunError procedure halts the program with an error status. RunError is not a real procedure.

Tips and Tricks

  • When a runtime error occurs, if ErrorProc is nil, Delphi calls RunError to print an error message and stop the program. In a GUI application, Delphi shows the error message in a dialog box. In a console application, Delphi prints the error message to the console.

  • If you write your own ErrorProc procedure, you can call RunError to halt the program and display a brief error message, but most likely you will want your ErrorProc procedure to do something different, such as raise an exception or print a different error message.

  • The ExitProc procedure and units’ finalization sections get to run before the program terminates.

  • Like Halt, RunError is a quick way to terminate a program, but not usually the right way for a GUI application. Instead, you should close the main form to terminate the application.

Example

// Report a run-time error. The address of the call to RunError
// is the error address. The address passed to ErrorProc is
// the address of the call to ErrorProc, which is close to,
// but not quite the same as, the address of the call to
// RunError. You can adjust the value returned by Caller,
// but that would be highly dependent on Delphi's code generator.

procedure ReportError(ErrorCode: Integer);
  function Caller: Pointer;
  asm
    MOV EAX, [ESP]
  end;
type
  TErrorProc = procedure(Code: Integer; Addr: Pointer);
begin
  if ErrorProc <> nil then
    TErrorProc(ErrorProc)(ErrorCode, Caller);
  RunError(ErrorCode);
end;

See Also

ErrorAddr Variable, ErrorProc Variable, ExitCode Variable, ExitProc Variable, Finalization Keyword, Halt Procedure, NoErrMsg Variable, SetInOutRes Procedure
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset