Why Application.OnException never runs?

前提是你 提交于 2020-01-17 17:15:03

问题


Problem summary: The method assigned to Application.OnException never runs when an unhandled exception occurs.

I create a blank project with only this unit and place a single button on Unit.dfm (this is based on an official example) :

// Unit1.pas
// *********

type
TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure AppException(Sender: TObject; E: Exception);
    procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
    Application.OnException := AppException;
end;

procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
    Application.Terminate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    raise Exception.Create('Incorrect password entered');
end;

Then I set a breakpoint inside TForm1.AppException(). I run the program, click the button, an error dialog is shown saying "Incorrect password entered" but if I continue execution the breakpoint never breaks; the program doesn't Terminate like I asked it too. The program continues running and I can press the button again.

I tried the same code (adapted) in Delphi 7 but the same result is encountered.


回答1:


The only rational explanation is the FormCreate is not executing. You need to assign it to the form's OnCreate event handler. Use the object inspector to do so.




回答2:


If you are using a third party exception handler such as madExcept, Application.OnException will no longer fire. You must instead follow the third party's advice. For madExcept you must code TMadExceptionHandler.OnException event or directly call RegisterExceptionHandler.



来源:https://stackoverflow.com/questions/30931967/why-application-onexception-never-runs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!