1
I am wanting to record in a text file any exception that happens on the system.
I’m using the component Application Events
of the Additional palette.
Below follows the example of the code:
procedure TfmPrototipo.ApplicationEventsException(Sender: TObject;
E:Exception);
var
NomeArquivo: string;
Arquivo: TextFile;
begin
NomeArquivo := ChangeFileExt(Application.Exename, '.log');
AssignFile(Arquivo, NomeArquivo);
if FileExists(NomeArquivo) then
Append(arquivo)
else
ReWrite(arquivo);
try
WriteLn(arquivo, 'Data: '+ DateTimeToStr(Now));
WriteLn(arquivo, 'Erro: ' + E.Message );
WriteLn(arquivo, '------------------------------------------- ');
Application.ShowException(E);
finally
CloseFile(arquivo);
end;
end;
The problem that in some screens of the systems is used the
try - except
, it displays the message and does not call the event onException
of the component.
I’m not aware of Delphi specifically, but in general
Application Events
is a global capturer of Exception not handled by the system, i.e., the ones that will generate crash in the system, the unknown problems in development. Already the errors where you use thetry - except
, are errors that you have already predicted in the development and possibly treated them in some way, so if this Exception is not propagated, it will not be captured byApplication Events
. So if despite treating the exception you want to log the same you should do it onexcept
oftry - except
.– Fernando Leal