The following code creates a SIGSEGV (on purpose, there is no control at that point).
How can I trap it. The try except below doesn't work.
var
aPoint : TPoint;
begin
aPoint.X := 5;
aPoint.Y := 80;
try
Caption := (Scrollbox1.ControlAtPos(aPoint, True, True) as TControl).Caption;
except
on E : Exception do
ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
end;
Why do you want to catch the exception when you easily can prevent it by checking against nil?
var
aPoint : TPoint;
C: TControl;
begin
aPoint := Point(5, 80);
C := Scrollbox1.ControlAtPos(aPoint, True, True); // no need to type-cast: ControlAtPos does return a TControl already
if C <> nil then
Caption := C.Caption
else
Caption := Format('No control found at point %d,%d', [aPoint.X, aPoint.Y]);
end;
If you insist on the exception you should be aware that normally every exception is caught by the IDE (unless you put the exception type on the ignore list in the "Language exceptions" of the project options), even if there is a try-except block. Only when the application is run outside the IDE you will notice that the exception is handled as expected.