3

Everytime I use a raise Exception.create('...');, it shows, differently from Delphi, the following box:

[my message]

Press OK to ignore and risk data corruption.
Press Cancel to kill the program.

I just want to change this default message and keep only my part.

Does someone know how can I do it?

felipe.zkn
  • 2,012
  • 7
  • 31
  • 63
  • 1
    You can assign your own method to Application.OnException, .. or handle the exception if possible... Don't know if there's another way. – Sertac Akyuz Jun 04 '13 at 01:13
  • 1
    @SertacAkyuz Can you show me an example? When I try to associate a handler to my application.onException, Lazarus seems to try to invoke the function and give me an error. – felipe.zkn Jun 04 '13 at 01:42
  • 2
    Let `procedure OnExcept(Sender : TObject; E : Exception);` is a method of a class, for instance of TForm1. Then you can write `Application.OnException := OnExcept;` – Sertac Akyuz Jun 04 '13 at 01:45
  • @SertacAkyuz It's very strange. I was doing the same thing you told. But Lazarus gives the error: Wrong number of parameters specified for call to "OnExcept". And I did it the private declarations too: --------------------------------- procedure onExcept(sender: TObject; e: Exception); --------------------------------- procedure TfrmMain.formCreate(sender: TObject); begin application.onException := onExcept; end; --------------------------------- procedure TfrmMain.onExcept(sender: TObject; e: Exception); begin //... end; – felipe.zkn Jun 04 '13 at 13:26
  • 1
    Now I just did: @onExcept; and it compiled. Accordingly to Lazarus documentation: Delphi users often confuse this, because Delphi allows it and adds the @ internally. If you prefer the Delphi syntax you can use {$mode Delphi} instead of {$mode ObjFPC}. – felipe.zkn Jun 04 '13 at 13:42
  • @SertacAkyuz, if you want you can answer the question. – felipe.zkn Jun 04 '13 at 13:43
  • 2
    You go ahead please :), if I'd answered I wouldn't even mention about the address operator. – Sertac Akyuz Jun 04 '13 at 17:39
  • @SertacAkyuz The merit is yours! Thanks to help me with the solution. – felipe.zkn Jun 04 '13 at 18:17

1 Answers1

2

To configure my own exception message, I did the following:

In the private declarations of application's main form:

procedure onExcept(sender: TObject; e: Exception);

In the OnCreate event of the main form:

procedure TfrmMain.formCreate(sender: TObject);
begin
    application.onException := @onExcept;
end;

procedure TfrmMain.onExcept(sender: TObject; e: Exception);
begin
    //...
end;

It's important to note that the @ operator is required if you're using Lazarus. If I didn't put it, the compiler would consider onExcept as a function call. Delphi adds it internally, so you don't have to worry about it.

If you want to change this behavior, use {$mode Delphi} instead of {$mode ObjFPC} directive.

felipe.zkn
  • 2,012
  • 7
  • 31
  • 63