Recent

Author Topic: [SOLVED] try/except block not catching  (Read 1587 times)

Slyde

  • Full Member
  • ***
  • Posts: 152
[SOLVED] try/except block not catching
« on: October 14, 2022, 06:04:31 pm »
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][+][-]
  1. var
  2.   count, a, i, k, x: Integer;
  3.   isDirty: Bool;
  4.   hasNumber: Bool;
  5. begin
  6.   try
  7.     shuffleVec.Free; // Global vector
  8.     shuffleVec := iVector.Create;
  9.     Memo1.Clear;
  10.  
  11.     ZQ1.Close;
  12.     ZQ1.SQL.Text := 'SELECT COUNT(*) FROM MC_Table';
  13.     ZQ1.Open;
  14.     count := 0;//ZQ1.Fields[0].AsInteger;
  15.  
  16.     {-- This effectively catches count = 0. but for some strange
  17.         reason, it gets called twice. --}
  18.     //if count = 0 then
  19.     //  begin
  20.     //    ShowMessage('The database is missing');
  21.     //    Exit;
  22.     //  end;
  23.  
  24.     a := 0;
  25.     repeat
  26.       isDirty := False;
  27.       hasNumber := False;
  28.  
  29.       {-- Random() crashes when count = 0.  But my try/except block
  30.           doesn't handle the crash.  So I'm thinking I don't know
  31.           how to properly catch this kind of error --}
  32.       x := Random(count)+1;
  33.  
  34.       if a = 0 then
  35.         begin
  36.           shuffleVec.PushBack(x);
  37.           hasNumber := True;
  38.           Inc(a)
  39.         end
  40.       else
  41.         for k := 0 to a do
  42.           if x = shuffleVec[k] then
  43.             isDirty := True;
  44.  
  45.       if not hasNumber then
  46.         if not isDirty then
  47.           begin
  48.             shuffleVec.PushBack(x);
  49.             Inc(a);
  50.           end;
  51.  
  52.     until a = count;
  53.   except
  54.     On e: Exception do
  55.        MessageDlg('Error' + e.Message, mtError,[mbOK],0);
  56.   end;
  57.  
  58.   // Show the vector's contents
  59.   for i := 0 to count-1 do
  60.     Memo1.Lines.Add(IntToStr(shuffleVec[i]));
  61. end;
« Last Edit: October 14, 2022, 11:02:35 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

jamie

  • Hero Member
  • *****
  • Posts: 7891
Re: try/except block not catching
« Reply #1 on: October 14, 2022, 06:10:05 pm »
Do not run app from ide in debug state. Run without debugger.
The only true wisdom is knowing you know nothing

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: try/except block not catching
« Reply #2 on: October 14, 2022, 06:58:01 pm »
Do 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:
« Last Edit: October 14, 2022, 07:02:46 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

wp

  • Hero Member
  • *****
  • Posts: 13646
Re: try/except block not catching
« Reply #3 on: October 14, 2022, 07:39:41 pm »
Code: Pascal  [Select][+][-]
  1.       {-- Random() crashes when count = 0.  ...}
  2.  x := Random(count)+1;
  3.  
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

  • Full Member
  • ***
  • Posts: 152
Re: try/except block not catching
« Reply #4 on: October 14, 2022, 07:58:31 pm »
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.
« Last Edit: October 14, 2022, 08:09:53 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

wp

  • Hero Member
  • *****
  • Posts: 13646
Re: try/except block not catching
« Reply #5 on: October 14, 2022, 08:07:02 pm »
This code does not crash on Mint either:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   n: Integer;
  4. begin
  5.   n := Random(0);
  6.   ShowMessage(IntToStr(n));
  7. end;

wp

  • Hero Member
  • *****
  • Posts: 13646
Re: try/except block not catching
« Reply #6 on: October 14, 2022, 10:32:34 pm »
So 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

  • Hero Member
  • *****
  • Posts: 7891
Re: try/except block not catching
« Reply #7 on: October 14, 2022, 10:42:14 pm »
Do 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.

The only true wisdom is knowing you know nothing

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: try/except block not catching
« Reply #8 on: October 14, 2022, 11:02:13 pm »
@jamie - that was a tricky one  :D   I didn't see it either, not until I read your response.  Good eye.  Thanks.
Linux Mint 21.3
Lazarus 3.0

 

TinyPortal © 2005-2018