So, I made a thorough test. The following things were mentioened: GUI or not GUI, Is there a try..except structure or not, SysUtils is added to the uses or not, fpSignal is defined or not. It gave me 16 combinations. I used the following two programs (with commenting out the parts not needed for a test:
program projectt;
uses
BaseUnix
, SysUtils
;
procedure signal_callback_handler(signum : integer); cdecl;
begin
Writeln('Signal caught ', SigNum);
end;
var
a, b, c : double;
begin
fpsignal(SIGFPE, SignalHandler(@signal_callback_handler));
a := 1;
b := 0;
try
c := a / b;
except
Writeln('Except caught');
end;
Writeln(c);
Readln;
end.
and
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
BaseUnix, Classes,
SysUtils,
Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class( TForm)
Button1 : TButton;
LabelFinish : TLabel;
LabelC : TLabel;
LabelExcept : TLabel;
LabelSignal : TLabel;
procedure Button1Click( Sender : TObject);
end;
var
Form1 : TForm1;
implementation
{$R *.lfm}
procedure signal_callback_handler(signum : integer); cdecl;
begin
Form1.LabelSignal.Caption := 'Signal caught';
end;
{ TForm1 }
procedure TForm1. Button1Click( Sender : TObject);
var
a, b, c : double;
begin
fpsignal(SIGFPE, SignalHandler(@signal_callback_handler));
a := 1;
b := 0;
try
c := a / b;
except
LabelExcept.Caption := 'Except caught';
end;
if C = 1 then
LabelC.Caption := 'LabelC reached';
LabelFinish.Caption := 'LabelFinish reached';
end;
end.
I got the following results:
GUI Try SysU fps Result
No No No No Runtime error 208
No No No Yes Infinite loop with SigNum 8
No No Yes No Unhandled exception Div0
No No Yes Yes Infinite loop with SigNum 8
No Yes No No Except catches properly
No Yes No Yes Infinite loop with SigNum 8
No Yes Yes No Except catches properly
No Yes Yes Yes Infinite loop with SigNum 8
Yes No No No Finishes, No Signal, No Except, No Error
Yes No No Yes Finishes, No Signal, No Except, No Error
Yes No Yes No Finishes, No Signal, No Except, No Error
Yes No Yes Yes Finishes, No Signal, No Except, No Error
Yes Yes No No Finishes, No Signal, No Except, No Error
Yes Yes No Yes Finishes, No Signal, No Except, No Error
Yes Yes Yes No Finishes, No Signal, No Except, No Error
Yes Yes Yes Yes Finishes, No Signal, No Except, No Error
- So if it is a GUI application, nothing matters. I find it very strange, however as my program is command line, it was not part of the original question. Still, does it mean that in Linux a GUI can never detect anyway a Div0 (or other serious) error? (8 cases covered)
- From the remaining 8 command line versions, if the fpSignal is set then regardless of the other two settings, it always ended up in an infinite loop. Again, I do not understand. At what point does it turn back in code and starts running again (i.e. a loop)? (4 more cases covered)
- If fpSignal is not set, but the try..except is set, then it is captured by the Except. The difference between SysUtils added or not is whether
can be used or not. This is as expected. (2 cases covered).
- If not even try..except is switched on then the program crashes. If SysUtil is not added the with a RunTime error 8 (I guess it is the OS Code), while if SysUtils is added then it catches it, coverts it to text and then crashes with unhandled Div0 error. Again this is as expected. (the last 2 cases covered).
So after all it seems that GUI overwrites everything, then it a command line program fpSignal overwrites everything, and SysUtils and try..except is only used otherwise. What bothers me now are the GUI and the infinite loop of fpSignal.