11 Vote

Delphi: Global Treatment of Errors (Exceptions)

Tutorial by Stefan Trost | Last update on 2023-11-20 | Created on 2012-08-15

With the help of Try-Except blocks, Delphi and Lazarus give us an easy way to catch and handle exceptions. If in the following code an error between "try" and "except" occurs, the code behind the "except" will be executed and we can prevent our application from showing a default error message to the user and implement our own error processing instead:

   // code
   on E:Exception do begin
     // will be executed when there is an error

However, if you desire a global error handler for all exceptions that occur somewhere in the program, Try-Except quickly reaches its limits. Finally, it would be too cumbersome to write any code of the program into a try-except block in order to redirect the error to a central function from there.

For this reason, I want to show you a way in this tutorial, how you can catch all errors occuring in your application in a single procedure to handle the exceptions there centrally.

Global Error Treatment

We call our procedure GlobalExceptionHandler and we declare it under "public" in this way:

  procedure GlobalExceptionHandler(Sender: TObject; E: Exception);

Our application knows the event "OnException" that is fired when an exception occurs. We want to overwrite this event with our own procedure, so we are writing this code with the following assignment into "OnCreate":

procedure TForm1.FormCreate(Sender: TObject);
  // Code for Delphi:
  Application.OnException := GlobalExceptionHandler;

  // Code for Lazarus:
  Application.OnException := @GlobalExceptionHandler;

Depending on whether we use Delphi or Lazarus for programming, we have to either assign the procedure with a preceding "@" character (Lazarus) or without (Delphi).

Finally, we only have to define our procedure GlobalExceptionHandler:

procedure TForm1.GlobalExceptionHandler(Sender: TObject; E: Exception);
  // this code will be executed in the case of an exception  

  // for example we could show a message...

  // ...or we can collect the error message with date and time in a memo
  Memo1.Lines.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss', now) + ' ' + E.Message);

This procedure is now called, whenever an error somewhere in our program encounters which triggers an exception.

Within the procedure, we can gather more information about the error. In E.ClassName, the type of our exception is written and the error message can be retrieved from E.Message. So, with this, you can define a different error handling for each type of error or we can simply write E.ClassName and E.Message to something like an error log and save it or whatelse you can imaging. In our example, we simply show the error message via a ShowMessage dialog and log the errors with date and time in a Memo field.

Raise own Exceptions

Of course, this type of global error treatment only works for the errors within our program, which trigger an exception (automatically only for errors such as divisions by zero, access violations and so on). If we also want to treat errors in our own code in our global exceptional handler, we can simply trigger our own exception. This works as follows:

raise Exception.Create('Arbitrary text of the error message');  

When we execute this line of code, an exception is created, which will then be passed on to our exception handler procedure and can be evaluated there. We can pass any text as an error message, for example, more information about the error, like which incorrect values or user inputs led to the problem or which occurrences were otherwise responsible for the exception.


About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile


Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.