Question: end.
I have a TThread object which may raise an exception in the Execute procedure. When an exception is raised, I want to be able to show that exception to the end user. How do I go about doing this in the most efficient way?
Answer:
With a TThread object, if you don't catch an exception in the Execute procedure of a TThread, you may get access violations. The Delphi IDE may break fine on the exception, but often when the application is run outside of the IDE you get an "Application error has occurred" exception and your application stops running.
If you don't care about showing the end user that an exception occurred, you can simply wrap your Execute procedure with a try..finally block such as:
procedure TMyThread.Execute;
begin
try
// Do your thread stuff here
except // Eat all exceptions
end;
end;
Quite often, this isn't the best solution and you will want to show the message to the end user, or allow your application to further process the message.
The easiest way to do this, is to add an Exception object to your TThread class, and call the appropriate handler based on the type of exception. Here is an example of how to do this. The project consists of one form with a Button placed on it:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
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 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;
begin
FException := nil;
try
// 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;
procedure TForm1.Button1Click(Sender: TObject);
begin
// Create an instance of the TMyThread
with TMyThread.Create(True) do
begin
FreeOnTerminate := True;
Resume;
end;
end;
Subscribe to:
Post Comments (Atom)
air max, burberry outlet, true religion jeans, louboutin, louis vuitton outlet, louboutin, ray ban sunglasses, tiffany and co, coach outlet store online, oakley sunglasses cheap, christian louboutin shoes, longchamp outlet, louis vuitton, true religion jeans, tory burch outlet, kate spade outlet, kate spade handbags, oakley sunglasses, jordan shoes, polo ralph lauren outlet, louis vuitton outlet, louis vuitton handbags, coach outlet, prada outlet, ray ban sunglasses, chanel handbags, michael kors outlet, longchamp handbags, michael kors outlet, oakley sunglasses, nike shoes, air max, coach purses, michael kors outlet, longchamp handbags, louis vuitton outlet stores, nike free, prada handbags, coach factory outlet, gucci outlet, tiffany and co, louboutin outlet, michael kors outlet, polo ralph lauren outlet, michael kors outlet, burberry outlet, michael kors outlet
louboutin, moncler outlet, juicy couture outlet, iphone 6 cases, hollister, moncler, karen millen, oakley, baseball bats, wedding dresses, ray ban, gucci, canada goose, ralph lauren, canada goose, swarovski crystal, hollister clothing store, moncler, air max, converse, pandora jewelry, juicy couture outlet, rolex watches, moncler, supra shoes, moncler, pandora charms, ugg, vans, toms shoes, links of london, pandora charms, air max, timberland boots, parajumpers, hollister, thomas sabo, louis vuitton, montre homme, coach outlet store online, lancel, ugg, swarovski, moncler, converse shoes