Recent

Author Topic: try except fails inside while loop [solved]  (Read 3297 times)

bigDan

  • New Member
  • *
  • Posts: 43
    • BigDanzBlog
try except fails inside while loop [solved]
« on: May 25, 2021, 10:21:12 pm »
I'm using Win-64 2.0.12 of Lazarus. This is the first time I've used this version of the compiler and I believe the first time I've used the 64bit windows compiler.

I am having a very odd issue. I'm not sure if I'm doing something completely lame that I just don't see or if there is a compiler problem.

The gist of the problem is I want to process an exception inside a while loop and keep on processing more data:

Code: Pascal  [Select][+][-]
  1. while <cond> do begin
  2.   try
  3.     someprocedure; // this can generate an exception
  4.   except
  5.     <handle error>
  6.   end;
  7. end;
  8.  

The problem is, when the exception occurs, I never get to <handle error>, the program just stops.

If I move the try/except to enclose the entire loop, that exception is processed correctly but now the while loop has been terminated by the exception.

Enclosed is a simple test program that produces what I am seeing. Use {$undefine works} and it will compile the code with the try/except  inside the loop - the code I want to have work.

In the {$undefined works} code, I would expect to see both 'failed', and 'after try' but neither are displayed.

Use {$define works} and it will compile code with the while loop enclosed by try/except. This works, but isn't what I need.

I have used exceptions often in the past, including my own. The only thing I can think I'm doing different than in the past is I'm testing the exception in the outblock, not a called procedure.

---------------------------------
Some additional information (added after initial post):

compiled the test program on another PC which is using 32bit Lazarus 2.0.10/FPC 3.2.0 and I am seeing the results I would expect:

after try
failed
after try
failed
...

The compiler with the problem is 64bit Lazarus 2.0.12 / FPC 3.2.0.

Same FPC compiler, different results. Is this a 64bit issue?
« Last Edit: May 27, 2021, 03:00:58 am by bigDan »

bigDan

  • New Member
  • *
  • Posts: 43
    • BigDanzBlog
Re: try except fails inside while loop
« Reply #1 on: May 25, 2021, 10:57:31 pm »
I have installed 32bit Lazarus 2.0.12 / FPC 3.2.0 on the original system exhibiting the problem. The test program works fine now, as does the program in question.

I cannot find any bugs with try / while. Not sure if this is a known problem or new.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7869
  • Debugger - SynEdit - and more
    • wiki
Re: try except fails inside while loop
« Reply #2 on: May 25, 2021, 11:29:10 pm »
Afaik 32 bit (win) still uses the "old" exception system.
64bit uses SEH exceptions.

So there is a difference.

I also get the error with the 3.2.2RC (I have not yet installed the final release.

I would say it is a bug in the compiler.

The asm output shows
Code: [Select]
.seh_handler __FPC_specific_handler,@except,@unwind
.seh_handlerdata

.section .text.n_main,"x"
.seh_endproc
And I am not sure, but I thought there ought to be data after seh_handlerdata
(There is in the working version).



Seems to work in fpc trunk
« Last Edit: May 25, 2021, 11:32:23 pm by Martin_fr »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9987
  • FPC developer.
Re: try except fails inside while loop
« Reply #3 on: May 25, 2021, 11:46:00 pm »
Afaik 32 bit (win) still uses the "old" exception system.
64bit uses SEH exceptions.

FPC < 3.2.0 is as you described. FPC 3.2.0+ both have SEH.

Bart

  • Hero Member
  • *****
  • Posts: 4689
    • Bart en Mariska's Webstek
Re: try except fails inside while loop
« Reply #4 on: May 26, 2021, 10:29:13 pm »
It fails with win64 if any optimization is used.
It works with -O-.

Same happens in fpc 3.0.4 and fpc 3.2.0 b.t.w.
So at least it's not a regression.

If you change "while true" to something that is actually calculated at runtime, it works with any optimazation level.

In trunk it works OK.

Bart

bigDan

  • New Member
  • *
  • Posts: 43
    • BigDanzBlog
Re: try except fails inside while loop
« Reply #5 on: May 27, 2021, 03:00:08 am »
Thanks. My project will be ported to a raspberry pi next week so it will become a moot point for me and looks like it is fixed for the next release.

Bart

  • Hero Member
  • *****
  • Posts: 4689
    • Bart en Mariska's Webstek
Re: try except fails inside while loop [solved]
« Reply #6 on: May 27, 2021, 12:41:06 pm »
It is however unclear if it will be fixed in the next point-release: 3.2.4.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 4689
    • Bart en Mariska's Webstek
Re: try except fails inside while loop [solved]
« Reply #7 on: May 29, 2021, 12:45:18 pm »
Does anybody know in what revision this was fixed and wether or not there was a bugreport about it?

Bart

 

TinyPortal © 2005-2018