Page 1 of 1

Raise Exception in thread

PostPosted: Thu Oct 24, 2019 7:09 pm
by dredfil
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);
  i: word;
  E: Exception;
  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


Re: Raise Exception in thread

PostPosted: Thu Oct 31, 2019 4:34 pm
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().