Note that there are some explanatory texts on larger screens.

plurals
  1. PORaising Exception in TThread Execute?
    text
    copied!<p>I just realized that my exceptions are not being shown to the user in my threads!</p> <p>At first I used this in my thread for raising the exception, which does not work:</p> <pre><code>except on E:Exception do begin raise Exception.Create('Error: ' + E.Message); end; </code></pre> <p>The IDE shows me the exceptions, but my app does not!</p> <p>I have looked around for a solution, this is what I found:</p> <p><a href="https://stackoverflow.com/questions/3627743/delphi-thread-exception-mechanism">Delphi thread exception mechanism</a></p> <p><a href="http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22039681.html" rel="nofollow noreferrer">http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22039681.html</a></p> <p>And neither of these worked for me.</p> <p>Here's my Thread unit:</p> <pre><code>unit uCheckForUpdateThread; interface uses Windows, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, GlobalFuncs, Classes, HtmlExtractor, SysUtils, Forms; type TUpdaterThread = class(TThread) private FileGrabber : THtmlExtractor; HTTP : TIdHttp; AppMajor, AppMinor, AppRelease : Integer; UpdateText : string; VersionStr : string; ExceptionText : string; FException: Exception; procedure DoHandleException; procedure SyncUpdateLbl; procedure SyncFinalize; public constructor Create; protected procedure HandleException; virtual; procedure Execute; override; end; implementation uses uMain; { TUpdaterThread } constructor TUpdaterThread.Create; begin inherited Create(False); end; procedure TUpdaterThread.Execute; begin inherited; FreeOnTerminate := True; if Terminated then Exit; FileGrabber := THtmlExtractor.Create; HTTP := TIdHTTP.Create(nil); try try FileGrabber.Grab('http://jeffijoe.com/xSky/Updates/CheckForUpdates.php'); except on E: Exception do begin UpdateText := 'Error while updating xSky!'; ExceptionText := 'Error: Cannot find remote file! Please restart xSky and try again! Also, make sure you are connected to the Internet, and that your Firewall is not blocking xSky!'; HandleException; end; end; try AppMajor := StrToInt(FileGrabber.ExtractValue('AppMajor[', ']')); AppMinor := StrToInt(FileGrabber.ExtractValue('AppMinor[', ']')); AppRelease := StrToInt(FileGrabber.ExtractValue('AppRelease[[', ']')); except on E:Exception do begin HandleException; end; end; if (APP_VER_MAJOR &lt; AppMajor) or (APP_VER_MINOR &lt; AppMinor) or (APP_VER_RELEASE &lt; AppRelease) then begin VersionStr := Format('%d.%d.%d', [AppMajor, AppMinor, AppRelease]); UpdateText := 'Downloading Version ' + VersionStr; Synchronize(SyncUpdateLbl); end; finally FileGrabber.Free; HTTP.Free; end; Synchronize(SyncFinalize); end; procedure TUpdaterThread.SyncFinalize; begin DoTransition(frmMain.TransSearcher3, frmMain.gbLogin, True, 500); end; procedure TUpdaterThread.SyncUpdateLbl; begin frmMain.lblCheckingForUpdates.Caption := UpdateText; end; procedure TUpdaterThread.HandleException; begin FException := Exception(ExceptObject); try Synchronize(DoHandleException); finally FException := nil; end; end; procedure TUpdaterThread.DoHandleException; begin Application.ShowException(FException); end; end. </code></pre> <p>If you need more info just let me know.</p> <p>Again: The IDE catches all the exceptions, but my program does not show them.</p> <p>EDIT: It was Cosmin's solution that worked in the end - and the reason it didn't at first, was because I didn't add the ErrMsg variable, instead I just placed whatever the variable would contain into the Synchronize, which would NOT work, however I have NO idea why. I realized it when I had no other ideas, and I just messed around with the solutions.</p> <p>As always, the joke's on me. =P</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload