Recent

Author Topic: [SOLVED] Exception handling  (Read 2705 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 861
[SOLVED] Exception handling
« on: December 09, 2021, 10:31:26 am »
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.

Code: Pascal  [Select][+][-]
  1. var
  2.   aPoint : TPoint;
  3. begin
  4.   aPoint.X := 5;
  5.   aPoint.Y := 80;
  6.   try
  7.     Caption := (Scrollbox1.ControlAtPos(aPoint, True, True) as TControl).Caption;
  8.   except
  9.     on E : Exception do
  10.       ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
  11.   end;
  12.  
  13.  
« Last Edit: December 09, 2021, 10:55:33 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

bytebites

  • Sr. Member
  • ****
  • Posts: 440
Re: Exception handling
« Reply #1 on: December 09, 2021, 10:39:16 am »
Avoid try except
Code: Pascal  [Select][+][-]
  1. acontrol:=Scrollbox1.ControlAtPos(aPoint, True, True);
  2. if (acontrol<>nil) and (acontrol is TControl)) then ..

pcurtis

  • Hero Member
  • *****
  • Posts: 861
Re: Exception handling
« Reply #2 on: December 09, 2021, 10:54:44 am »
Thanks
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 9174
Re: [SOLVED] Exception handling
« Reply #3 on: December 09, 2021, 11:16:29 am »
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.

Code: Pascal  [Select][+][-]
  1. var
  2.   aPoint : TPoint;
  3. begin
  4.   aPoint.X := 5;
  5.   aPoint.Y := 80;
  6.   try
  7.     Caption := (Scrollbox1.ControlAtPos(aPoint, True, True) as TControl).Caption;
  8.   except
  9.     on E : Exception do
  10.       ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
  11.   end;
  12.  
  13.  
Why do you want to catch the exception when you easily can prevent it by checking against nil?
Code: Pascal  [Select][+][-]
  1. var
  2.   aPoint : TPoint;
  3.   C: TControl;
  4. begin
  5.   aPoint := Point(5, 80);
  6.   C := Scrollbox1.ControlAtPos(aPoint, True, True);  // no need to type-cast: ControlAtPos does return a TControl already
  7.   if C <> nil then
  8.     Caption := C.Caption
  9.   else
  10.     Caption := Format('No control found at point %d,%d', [aPoint.X, aPoint.Y]);
  11. end;
  12.  
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.
« Last Edit: December 09, 2021, 11:18:33 am by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

pcurtis

  • Hero Member
  • *****
  • Posts: 861
Re: [SOLVED] Exception handling
« Reply #4 on: December 09, 2021, 11:51:26 am »
Isn't exception handling about capturing unexpected things?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 9174
Re: [SOLVED] Exception handling
« Reply #5 on: December 09, 2021, 12:17:39 pm »
Isn't exception handling about capturing unexpected things?
When you work with pointers (this is what class instances are in the end) you always must be aware of them being nil. So, this is nothing "unexpected".
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

PascalDragon

  • Hero Member
  • *****
  • Posts: 3668
  • Compiler Developer
Re: [SOLVED] Exception handling
« Reply #6 on: December 09, 2021, 01:12:54 pm »
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.

Does it work if you don't run it from the IDE? (aka without a debugger as wp had mentioned as well)


 

TinyPortal © 2005-2018