Program crashes on Windows 11 within Try Except lazarus / 34-Windows ---------------------------------------------------------------------- gicla · 2023-12-10 16:27 Hi everybody, After days trying to understand I decided to post here as I'm lost. My program always ran fine on Windows10. Since a few months I upgraded to Windows 11 and now at all the Try...Except, in cas of errors it freezes instead of executing the Except clause. Any hint please? That's the code which hangs: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // Verify Fields value type for M fields and MF or MD whitin approved messages      case UpperCase(copy(aFieldsDef[3, z], 1, 3)) of          'INT' : begin                    try                      iTestValue := StrToInt64(AnsiString(xmlDoc.DocumentElement.FindNode(aFieldsDef[1, z]).TextContent));                    except                      WriteError('Line: ' + sLine + ' | ' + xmlStr.DataString, 'ERROR: "' + AnsiString(aFieldsDef[1, z]) + '" value MUST be numeric, only digits allowed here.');                    end;                  end;        'STR' : begin                   end;          'CON' : begin     { TODO : da eliminare e gestire }                   end;          else MessageDlg('(' + AnsiString(sMsgType) + ') ' + AnsiString(aFieldsDef[1, z]) + ' - Wrong field type definition ' + '(' + UpperCase(copy(aFieldsDef[3, z],1,3)) + ')' + ' into the ini file.' + #10 + 'Allowed values are:' + #10 + '"STR" = String' + #10 + '"INT" = Numeric', mtWarning, [mbOK], 0);        end;  I tried to inser the following test code in teh same procedure which hangs and it also hangs: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   divider := '0';  Try    Result := 5 div StrToInt(divider);  Except    showmessage('0 divide not possible');  end; However, the same sample code used in a different procedure, works just fine. ---------------------------------------------------------------------- rvk · 2023-12-10 16:34 So what does WriteError do? What happens if there is an exception in your except clause? What happens after this code? Just change the writeerror to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i on E: exception do showmessage(e.message);and you'll see the actual reason for the exception. (Or run in the IDE and let it catch the exception) ---------------------------------------------------------------------- Thaddy · 2023-12-10 17:52 Note EDivideByZero is one of the standard exceptions defined in sysutils.ou do noy want to rely on sysutils, because of its size, you can check for error 200. ---------------------------------------------------------------------- PascalDragon · 2023-12-10 20:18 Quote from: Thaddy on December 10, 2023, 06:52:08 pmNote EDivideByZero is one of the standard exceptions defined in sysutils.ou do noy want to rely on sysutils, because of its size, you can check for error 200. On Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. ---------------------------------------------------------------------- Bart · 2023-12-10 21:05 Quote from: PascalDragon on December 10, 2023, 09:18:19 pmOn Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. Is that documented somewhere? Bart ---------------------------------------------------------------------- dbannon · 2023-12-10 22:47 Quote from: PascalDragon on December 10, 2023, 09:18:19 pmOn Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. Wow, that rings alarm bells, especially for Windows avoiders like me. SEH is a compiler option to Microsoft C (and incomplete with C++), so a FPC/Lazarus app would not be using SEH. Even MS seem to suggest its not a good thing. Is the warning from Pascal Dragon because we might be linked to libraries that do use SEH ? By " not fully supported" do you mean it compiles but might not work ?  Does the compiler generate a Warning for example ?  Given that I do much of my "What if this happens" extreame testing in Linux, sounds like the exceptional events that might lead to an Exception might just crash my app on Windows unpredictably ? Worried from Axedale (Davo) ---------------------------------------------------------------------- avra · 2023-12-11 06:22 Quote from: gicla on December 10, 2023, 05:27:23 pmThat's the code which hangs:If you delete line 16 does it still hang? ---------------------------------------------------------------------- ASerge · 2023-12-11 20:59 Code uses Dialogs.MessageDlg. Unit Dialogs include SysUtils, therefore, thoughts about the fact that SEH does not work without Sysutils in this topic can be discarded. We need a minimal example of where the error would occur so that others can check it. Maybe the problem is elsewhere. ---------------------------------------------------------------------- PascalDragon · 2023-12-14 20:54 Quote from: Bart on December 10, 2023, 10:05:03 pmQuote from: PascalDragon on December 10, 2023, 09:18:19 pmOn Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. Is that documented somewhere? No, because it's essentially a bug. Quote from: dbannon on December 10, 2023, 11:47:13 pmQuote from: PascalDragon on December 10, 2023, 09:18:19 pmOn Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. Wow, that rings alarm bells, especially for Windows avoiders like me. SEH is a compiler option to Microsoft C (and incomplete with C++), so a FPC/Lazarus app would not be using SEH. Even MS seem to suggest its not a good thing. Is the warning from Pascal Dragon because we might be linked to libraries that do use SEH ? Ehm... you are aware that SEH is the exception handling mechanism on Windows, used throughout the operating system and even across languages? The compiler option in MSVC is simply whether the conversion of SEH exceptions to C++ exceptions is enabled. There's a reason after all that we switched from setjmp/longjmp based exception handling, because otherwise there were issues with exceptions that occurred in libraries that the executable captured while it shouldn't. Quote from: dbannon on December 10, 2023, 11:47:13 pmBy " not fully supported" do you mean it compiles but might not work ?  Does the compiler generate a Warning for example ?  Given that I do much of my "What if this happens" extreame testing in Linux, sounds like the exceptional events that might lead to an Exception might just crash my app on Windows unpredictably ? It means that it will compile, but an except-block might not be entered correctly if at all if an exception occurs. ---------------------------------------------------------------------- dbannon · 2023-12-15 10:31 Quote from: PascalDragon on December 14, 2023, 09:54:52 pm.... Ehm... you are aware that SEH is the exception handling mechanism on Windows, used throughout the operating system and even across languages? The compiler option in MSVC is simply whether the conversion of SEH exceptions to C++ exceptions is enabled. Nope !  I don't use Windows, I most certainly don't do any low level programming under windows. Thats why I use FPC/Lazarus. Quote from: PascalDragon on December 14, 2023, 09:54:52 pm..... It means that it will compile, but an except-block might not be entered correctly if at all if an exception occurs. Like I said, Wow. So, quick grep through my code, only 36 "except on", less than I'd expect. But quite a variety.  Is there any point in testing ?  I mean, if I simulate, say an EXMLReadError and its correctly handled, can I assume it would always be correctly handled ? David ---------------------------------------------------------------------- gicla · 2023-12-15 14:05 Thank you all for your time and responses, let me give you some more info below: @rvk The "WriteError" procedures works just fine. When the program doesn't hang, due to the try...exept issue, WriteError does what it should do. When there is an exception, instead of executing the code after "Except", the program hangs displaying the attached error. If I move the same Try...Except code outside the procedure where it should run, it works as expected executing the "Except" clause . @ASerge is correct. SysUtils is included ---------------------------------------------------------------------- gicla · 2023-12-15 14:22 Let me add some additional info which, to me, makes this issue a little weirder: The following code make the application hanging when executing the "Except" clause: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.VerifyMandatoryFields;var  z, x, i, iSLIdx, iDummy : Integer;  iTestValue, Mytest : Int64;  sTmpNodeName : WideString;  sFldDim : String;  bDccChecked : Boolean;  OPIExpDate : TDateTime;  ExRate : Real;  dDummy : Double;begin  try    MyTest := StrToInt64('12z3568');  except    showmessage('Not a valid Int64 value');  end;end;  While the following code works just fine: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.VerifyMandatoryFields;var  z, x, i, iSLIdx, iDummy : Integer;  iTestValue, Mytest : Int64;  sTmpNodeName : WideString;  sFldDim : String;  bDccChecked : Boolean;  OPIExpDate : TDateTime;  ExRate : Real;  dDummy : Double;begin  if not TryStrToInt64('12z3568', Mytest) then showmessage('Not a valid Int64 value');end; ---------------------------------------------------------------------- rvk · 2023-12-15 14:37 When and by what code is TForm1.VerifyMandatoryFields called? What happens if you put this code in a Button1Click under a button? Does it hang then too? (I'm wondering if this code isn't called sitting a thread or something.) ---------------------------------------------------------------------- gicla · 2023-12-15 16:56 Quote from: rvk on December 15, 2023, 03:37:26 pmWhen and by what code is TForm1.VerifyMandatoryFields called? What happens if you put this code in a Button1Click under a button? Does it hang then too? (I'm wondering if this code isn't called sitting a thread or something.) TForm1.LoadButtonClick event calls | |--> TForm1.AnalyzeLog which in turn calls                         |                         |--> TForm1.VerifyXMLHeaderExist                         |--> TForm1.VerifyMandatoryFields ---------------------------------------------------------------------- rvk · 2023-12-15 16:57 If the code also crashes in a simple ButtonClick then you can make a small project which reproduces the problem and it can be submitted as bugreport. For me, this works without any problems on Linux Mint. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  Mytest : Int64;begin  try  MyTest := StrToInt64('12z3568');  except    showmessage('Not a valid Int64 value');  end;end; ---------------------------------------------------------------------- PascalDragon · 2023-12-19 21:27 Quote from: dbannon on December 15, 2023, 11:31:47 amQuote from: PascalDragon on December 14, 2023, 09:54:52 pm..... It means that it will compile, but an except-block might not be entered correctly if at all if an exception occurs. Like I said, Wow. So, quick grep through my code, only 36 "except on", less than I'd expect. But quite a variety.  Is there any point in testing ?  I mean, if I simulate, say an EXMLReadError and its correctly handled, can I assume it would always be correctly handled ? As soon as you have any descendant of the Exception class you don't need to worry anymore, because that will mean that the SysUtils unit will be used and thus everything will be set up correctly. The problem only occurs if the SysUtils unit isn't used anywhere in your whole program. ---------------------------------------------------------------------- dbannon · 2023-12-19 23:08 OK, thanks for that PD, that is a great relief, I always use SysUtils. As Bart asked, is this documented somewhere ?  Somewhere where the unwary might find it ?  It does sound like a real corner case but could cause someone a lot of trouble if they did replicate the conditions. Davo ---------------------------------------------------------------------- Thaddy · 2023-12-20 15:24 Yes this is documented all over the official documentation so I wonder how you could have missed it. Is this a case again of ask before you read? You should know better. This question does not belong here because it is wasting time from other people. ---------------------------------------------------------------------- dbannon · 2023-12-20 23:01 Quote from: Thaddy on December 20, 2023, 04:24:49 pmYes this is documented all over the official documentation so I wonder how you could have missed it. Oh, thats good then. Or is it ? Just to be sure, if Its "all over the official documentation" I suppose its really easy to find. I read through chapter 17 of the Ref, no mention of an unexpected behavior when using Windows to handle an Exception without SysUtils. That seems strange given its "All over".  I also looked in the Wiki and the Help offered when you press F1 in Lazarus. That last Help provided the closest I could find to what we are talking about here Thaddy - While it is possible to raise an exception with any class descending from TObject, it is recommended to use Exception as the basis of exception class objects: the Exception class introduces properties to associate a message and a help context with the exception being raised. What is more, the SysUtils unit sets the necessary hooks to catch and display unhandled exceptions: in such cases, the message displayed to the end user, will be the message stored in the exception class. But it does not mention a MSWindow specific problem, does not mention SEH and the fact that its "not supported anymore" QuoteIs this a case again of ask before you read? You should know better. This question does not belong here because it is wasting time from other people. No, its usual case of Thaddy sounding authoritative but being careful to not actually help at all. Here, I will show you how it is done - https://www.freepascal.org/docs-html/ref/refch17.html https://wiki.freepascal.org/Exceptions See how easy it is to include a link to something your correspondent clearly does not know about ? Give it a try some time. I have added some text to the Wiki page, but at this stage, I see no reason to believe that the problem Pascal Dragon alerted us to is well documented in the official documentation, I quote - QuoteOn Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. Davo ---------------------------------------------------------------------- Bart · 2023-12-21 11:48 Quote from: dbannon on December 21, 2023, 12:01:56 amI have added some text to the Wiki page, but at this stage, I see no reason to believe that the problem Pascal Dragon alerted us to is well documented in the official documentation, I quote - QuoteOn Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. And he goes on to say (when I asked wether this was documented): Quote from: PascalDragon on December 14, 2023, 09:54:52 pmNo, because it's essentially a bug. Bart ---------------------------------------------------------------------- PascalDragon · 2023-12-21 21:10 Quote from: dbannon on December 21, 2023, 12:01:56 amI have added some text to the Wiki page, but at this stage, I see no reason to believe that the problem Pascal Dragon alerted us to is well documented in the official documentation, I quote - QuoteOn Windows with SEH using try … except-blocks without using the SysUtils unit is currently (from 3.2.0 on) not fully supported anymore, because the SEH implementation is missing some functions to allow the usage without that unit. Because it's supposed to work. That it doesn't is a bug. Bugs are not going to be documented as part of the official documentation. ---------------------------------------------------------------------- dbannon · 2023-12-22 03:49 Yes, PascalDragon, I see the argument, if its documented in the official documentation, its like saying its a behavior you can depend on, it will stay like that. And that is clearly wrong. I will annotate the entry I put in the wiki to point out it is a bug and will, at some point in time be fixed at some level. For most users, those who 'use sysutils' it is a not a problem anyway, adding SysUtils will not adversely affect too many apps in my opinion. Davo