delphi thread exception 異常捕捉

unit UnitMyThread;

interface

uses
  System.Classes, System.SysUtils, vcl.Forms;

type
  TMyThread = class(TThread)
  private
    procedure DoHandleException;
    procedure HandleException;
    { Private declarations }
  protected
    procedure Execute; override;
  end;

implementation

var
  FException: Exception;

procedure TMyThread.DoHandleException;
begin

  // Now actually show the exception
  if FException is Exception then
    Application.ShowException(FException)
  else
    ShowException(FException, nil);
end;

procedure TMyThread.Execute;
begin
  FException := nil;
  try
    StrToFloat('');
    // raise an Exception
    raise Exception.Create('I raised an exception');
  except
    HandleException;
  end;
end;

procedure TMyThread.HandleException;
begin
  // This function is virtual so you can override it
  // and add your own functionality.
  FException := Exception(ExceptObject);
  try
    // Don't show EAbort messages
    if not(FException is EAbort) then
      Synchronize(DoHandleException);
  finally
    FException := nil;
  end;
end;

end.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章