[SOLVED] try/except block not catching lazarus / 21-General ---------------------------------------------------------------------- Slyde · 2022-10-14 16:04 Linux Mint 21 Lazarus 2.2.2 FPC 3.2.2 The code below works fine, until Random() is supplied with a zero.  It crashes on line 32, and my attempt to catch it fails.   I'm reading the number of table entries into the var count, and that shld never ever be 0.  But there's always the possibility the user cld rename the db, move it, or delete it.  And I don't want my program to just freeze and die on the spot.  I'm trying to handle that situation, however unlikely it might be, and the try/except block isn't doing that for me.  So I hope someone can show me the err of my ways so I can make this work. 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 var  count, a, i, k, x: Integer;  isDirty: Bool;  hasNumber: Bool;begin  try    shuffleVec.Free; // Global vector    shuffleVec := iVector.Create;    Memo1.Clear;     ZQ1.Close;    ZQ1.SQL.Text := 'SELECT COUNT(*) FROM MC_Table';    ZQ1.Open;    count := 0;//ZQ1.Fields[0].AsInteger;     {-- This effectively catches count = 0. but for some strange        reason, it gets called twice. --}    //if count = 0 then    //  begin    //    ShowMessage('The database is missing');    //    Exit;    //  end;     a := 0;    repeat      isDirty := False;      hasNumber := False;       {-- Random() crashes when count = 0.  But my try/except block          doesn't handle the crash.  So I'm thinking I don't know          how to properly catch this kind of error --}      x := Random(count)+1;       if a = 0 then        begin          shuffleVec.PushBack(x);          hasNumber := True;          Inc(a)      end      else        for k := 0 to a do          if x = shuffleVec[k] then            isDirty := True;       if not hasNumber then        if not isDirty then          begin            shuffleVec.PushBack(x);            Inc(a);          end;     until a = count;  except    On e: Exception do       MessageDlg('Error' + e.Message, mtError,[mbOK],0);  end;   // Show the vector's contents  for i := 0 to count-1 do    Memo1.Lines.Add(IntToStr(shuffleVec[i]));end; ---------------------------------------------------------------------- jamie · 2022-10-14 16:10 Do not run app from ide in debug state. Run without debugger. ---------------------------------------------------------------------- Slyde · 2022-10-14 16:58 Quote from: jamie on October 14, 2022, 06:10:05 pmDo not run app from ide in debug state. Run without debugger. With the IDE closed or open and ran without the debugger, I get the same thing as I do with the debugger: ---------------------------------------------------------------------- wp · 2022-10-14 17:39 Quote from: Slyde on October 14, 2022, 06:04:31 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       {-- Random() crashes when count = 0.  ...} x := Random(count)+1; Why? Random(0) does not crash here (Windows, Laz-main/FPC 3.2.2, Laz 1.6.4./FPC 2.6.4, Delphi XE 10.4). ---------------------------------------------------------------------- Slyde · 2022-10-14 17:58 No idea why it crashes, wp.  That's why I shared that bit of code, so you cld try it and see for yourselves.  You say it works on Windows.  I haven't tried there yet, but I will.  I'm coding this on Linux Mint 21, where the error's encountered.  Thanks for the heads up on Windows, though. And this really isn't even abt Random(); it's abt catching an error when thrown.  While this might not throw an error on Windows, it does on Linux Mint, as seen in the above image.  I say it's Random() only because I can change count := 0 to count := 1 and it doesn't error.  So I just wanted to know if I was using the try/except the correct way.  As for the error I'm getting, I already have that fixed.  But I'd rather rely on the try/catch block. ---------------------------------------------------------------------- wp · 2022-10-14 18:07 This code does not crash on Mint either: 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  n: Integer;begin  n := Random(0);  ShowMessage(IntToStr(n));end; ---------------------------------------------------------------------- wp · 2022-10-14 20:32 Quote from: Slyde on October 14, 2022, 07:58:31 pmSo I just wanted to know if I was using the try/except the correct way.  It seems to me that you expect the try/except block to "undo" an exception. No - the exception already has occured, you cannot make it undone. The only thing you can do is to tell the system what should happen in case of an exception. You simply want to display a messagebox. Well, that's not necessary, the automatic exception handler exactly does that. I do agree that the default exception handler offering to abort the application is rather intimidating; but when you drop a TApplicationProperties component on the form and set the ExceptionDialog property to aedOKMessageBox you have a normal messagebox - just what your except block is doing. ---------------------------------------------------------------------- jamie · 2022-10-14 20:42 Quote from: Slyde on October 14, 2022, 06:58:01 pmQuote from: jamie on October 14, 2022, 06:10:05 pmDo not run app from ide in debug state. Run without debugger. With the IDE closed or open and ran without the debugger, I get the same thing as I do with the debugger: I see now, it's not an error its a loop you are stuck in and your OS detects due to you not returning control back. ---------------------------------------------------------------------- Slyde · 2022-10-14 21:02 @jamie - that was a tricky one     I didn't see it either, not until I read your response.  Good eye.  Thanks.