码迷,mamicode.com
首页 > 编程语言 > 详细

线程中的异常处理

时间:2019-03-15 13:12:41      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:com   apt   final   ace   ESS   variant   use   cep   graphics   

// Quelle: http://edn.embarcadero.com/article/10452

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

  TMyThread = class(TThread)
  private
    FException: Exception;
    procedure DoHandleException;
  protected
    procedure Execute; override;
    procedure HandleException; virtual;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
  // Create an instance of the TMyThread
  with TMyThread.Create(True) do
  begin
    FreeOnTerminate := True;
    Resume; 
  end;
end;

{ TMyThread }

procedure TMyThread.DoHandleException;
begin
  // Cancel the mouse capture
  if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
  // Now actually show the exception
  if FException is Exception then
    Application.ShowException(FException)
  else
    SysUtils.ShowException(FException, nil);
end;

procedure TMyThread.Execute;
var
  a: Integer;
  b: Integer;
begin
  inherited;
  FException := nil;
  try
    // raise an Exception
    b := 0;
    a := 42 div b;
    Messagebox(0, PansiChar(IntToStr(a)), fehler, 0)
    //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.

 

线程中的异常处理

标签:com   apt   final   ace   ESS   variant   use   cep   graphics   

原文地址:https://www.cnblogs.com/kinglandsoft/p/10536326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!