Root > Reference > All Classes > TThreadEx > Properties > TThreadEx.AutoHandleException

Property TThreadEx.AutoHandleException

Previous pageReturn to chapter overviewNext page   

Allows you to enable automatic thread's exception handling.

 

Unit

EBase

 

Value

True - automatically invoke EurekaLog for exceptions in thread. False (default) - do not alter standard behaviour; store thread exception in FatalException property; you must analyze this exception manually.

 

Remarks

By default this property is set to False. This means that TThreadEx class will behave in the same way as TThread class - it will store thread's fatal exception into FatalException property, which must be analyzed by caller thread. For example:

Code (Delphi)

// Create thread

Thread := TMyThread.Create('Thread Name');

try

 

// Wait for thread's completion.

// This wait can be implemented in any other way.

// E.g. you can assign OnTerminate handler;

// or you can PostMessage from thread to main thread.

Thread.WaitFor;

 

// Analyze thread completion.

// Re-raise any thread error in current thread.

// You should do this only after the thread has finished.

E := Thread.FatalException;

if Assigned(E) then

begin

// clear FatalException property

PPointer(@Thread.FatalException)^ := nil;

raise E;

end;

 

finally

FreeAndNil(Thread);

end;

 

or:

 

Code (Delphi)

type

TForm1 = class(TForm)

...

private

procedure HandleThreadException(Sender: TObject);

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

Thread: TThreadEx;

begin

Thread := TThreadEx.Create(True);

Thread.OnTerminate := Self.HandleThreadException;

Thread.FreeOnTerminate := True;

Thread.Start;

Thread := nil; // never access thread var with FreeOnTerminate after Start

end;

 

procedure TForm1.HandleThreadException(Sender: TObject);

var

Thread: TThread;

begin

Thread := (Sender as TThread);

if Assigned(Thread.FatalException) then

HandleException(Thread.FatalException, False, atThread);

end;

 

 

 

When AutoHandleException property is set to True - EurekaLog will call HandleException function automatically for thread's fatal exceptions.

 

Code (Delphi)

procedure TForm1.Button1Click(Sender: TObject);

var

Thread: TThreadEx;

begin

Thread := TThreadEx.Create(True);

Thread.AutoHandleException := True;

Thread.FreeOnTerminate := True;

Thread.Start;

Thread := nil; // never access thread var with FreeOnTerminate after Start

end;

 

Please note that even when AutoHandleException is True - FatalException property will still be filled with exception.

 

See also

SetEurekaLogStateInThread Function



Send feedback... Build date: 2023-09-11
Last edited: 2023-09-11
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/topic_property_ebase_tthreadex_autohandleexception.php