Raise Exception in thread

delphi package - automated exception handling
Post Reply
dredfil
Posts: 1
Joined: Thu Oct 24, 2019 7:00 pm

Raise Exception in thread

Post by dredfil »

Hello!
In my project there are many objects of class TThread. Each of them has own log, that can be viewed in mainform.
Some of threads crash and madExcept processed exception, How i can also write exception message to log after MadExcept catch?

Code: Select all

procedure TMainForm.ThrOnTerm(Sender: TObject);
var
  i: word;
  E: Exception;
begin
  i:=TBotThread(Sender).num;
  if not TBotThread(Sender).DeleteBotData then begin
    E:=(Bot[i].thr.FatalException as Exception);
    if E<>nil then
      Bot[i].thr.log('Ошибка: '+E.ClassName+' '+E.Message,2);   ----- i write log here, but nothing catch
    Bot[i].BD.data.Status:='Выключен';
    Bot[i].thr:=nil;
  end;
end;

thr.OnTerminate:=ThrOnTerm;

madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Raise Exception in thread

Post by madshi »

Do you need access to thread-specific variables/resources, when writing to the log? One easy solution would be to use "RegisterExceptionHandler" and in your registered handler then write something to the log. However, the handler will not be called in the context of the crashing thread, so this may not be what you want? Alternatively, you could add a try..except block around each thread's Execute method and in the try..except block write something to the log, then do "raise" to re-raise the exception, so madExcept becomes active. Or alternatively, instead of using "raise" you could also directly call madExcept.HandleException().
Post Reply