Wenn du die
Exception im Execute nicht abfängst, dann kannst du sowas machen:
Delphi-Quellcode:
unit Unit6;
interface
uses
System.SysUtils, System.Classes,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TMyThread =
class(TThread)
protected
procedure Execute;
override;
end;
TForm6 =
class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FThread: TMyThread;
procedure OnMyThreadTerminate(Sender: TObject);
public
{ Public declarations }
end;
var
Form6: TForm6;
implementation
{$R *.dfm}
procedure TForm6.Button1Click(Sender: TObject);
begin
FThread := TMyThread.Create;
FThread.OnTerminate := OnMyThreadTerminate;
FThread.FreeOnTerminate := True;
end;
{ TMyThread }
procedure TMyThread.Execute;
var
a, b: Integer;
begin
inherited;
b := 0;
a := a
div b;
end;
procedure TForm6.OnMyThreadTerminate(Sender: TObject);
var
ThreadException:
Exception;
begin
// das hier wird im Kontext des Main-Threads automatisch aufgerufen.
if Assigned(Sender)
and (Sender
is TThread)
then
begin
ThreadException :=
Exception(TThread(Sender).FatalException);
if Assigned(ThreadException)
then
ShowMessage(ThreadException.Classname + '
: ' + ThreadException.
Message);
end;
end;
end.