I am writing an IPC application. I want to catch exceptions from process B silently and then send the exception details as string to process A. I am able to catch exceptions from main thread, but have problems to catch exceptions from a different thread.
procedure TForm1.ApplicationEvents1Exception(Sender: TObject; E: Exception);
begin
SendExceptionToAnotherProcess(E.ToString);
end;
type
AThread = class(TThread)
protected
procedure Execute; override;
end;
procedure AThread.Execute; // Main thread cannot catch such runtime exception
var uq1, uq2: UInt64;
s1: Single;
d1: Double;
begin
uq1 := $9000000000000000;
s1 := uq1; // no problem here
// exception class $C0000090 with message 'c0000090 FLOAT_INVALID_OPERATION'.
uq2 := round(s1); // but back-conversion crashes
end;
procedure TForm1.Button1Click(Sender: TObject);
var
T: AThread;
begin
T := AThread.Create(True);
T.Start;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
raise Exception.Create('I can catch it');
end;
Is there any way to catch exceptions from all threads? The exception might be thrown by 3rd library or ActiveX control in different threads.
I tried madExcept, which is able to catch exception from different thread. After further digging, I found a way to catch exceptions silently with the help of madExcept.
procedure TForm1.HandleUncaughtException(const ExceptIntf: IMEException; var Handled: Boolean);
begin
SendExceptionToAnotherProcess(ExceptIntf.ExceptMessage);
Handled := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
madExcept.RegisterExceptionHandler(HandleUncaughtException, stTrySyncCallAlways, epMainPhase);
end;