Recent

Author Topic: [SOLVED] How do I catch this database error?  (Read 3348 times)

Landslyde

  • Full Member
  • ***
  • Posts: 182
  • Jesus lives!
[SOLVED] How do I catch this database error?
« on: August 19, 2018, 06:23:45 pm »
Code: Pascal  [Select]
  1. procedure TForm1.Button_recipeIngredientsDeleteClick(Sender: TObject);
  2. begin
  3.   try
  4.       ShowMessage('Begin deletion query');
  5.                   Query_ingredients.SQL.Text := 'DELETE FROM recipe_ingredients WHERE ingredient_id = :id';
  6.                   Query_ingredients.ParamByName('id').AsInteger := Query_ingredients.FieldByName('ingredient_id').AsInteger;
  7.                   Query_ingredients.ExecSQL;
  8.                   dm1.Trans1.CommitRetaining;
  9.       ShowMessage('End deletion query');
  10.  
  11.             Button_ingredientsRefreshClick(Sender);
  12.   except
  13.     On e: EDatabaseError do begin
  14.        Exit;
  15.      end;
  16.   end;
  17. end;

I never make it to the second ShowMessage. ingredient_id field is empty, so it's throwing an error. And all I want to do is quietly Exit. Why is my try/except failing?
« Last Edit: August 19, 2018, 07:23:00 pm by Landslyde »
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.

rvk

  • Hero Member
  • *****
  • Posts: 3798
Re: How do I catch this database error?
« Reply #1 on: August 19, 2018, 06:28:26 pm »
Are you running with debugging on or off?
(Try running outside the IDE or "Run without debugging")

You might be able to disable this specific exception when you get it in the IDE so it doesn't trigger inside the IDE with debugging on. Use the checkmark in the exception when you get it.

Landslyde

  • Full Member
  • ***
  • Posts: 182
  • Jesus lives!
Re: How do I catch this database error?
« Reply #2 on: August 19, 2018, 06:42:13 pm »
It still throws outside the IDE. But it doesnt crash. Just get a possible data corruption warning. And that's fine, because the database is unaffected. But, and this is bigger for me, why is the try/catch not working? The error is clearly inside the try block, and the catch sends it on its way with an Exit. But it isnt working that way. Is there some kind of voodoo magic I need to issue to get error catching to work?
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.

Landslyde

  • Full Member
  • ***
  • Posts: 182
  • Jesus lives!
Re: How do I catch this database error?
« Reply #3 on: August 19, 2018, 06:55:10 pm »
Are you running with debugging on or off?
(Try running outside the IDE or "Run without debugging")

You might be able to disable this specific exception when you get it in the IDE so it doesn't trigger inside the IDE with debugging on. Use the checkmark in the exception when you get it.

Thanks, rvk.  I got it.  I've never messed with the debugger before, but I turned off the one checked option it had and now the finished doesnt throw the error.  It didnt need to anyway. Thanks again.
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.

af0815

  • Sr. Member
  • ****
  • Posts: 343
Re: How do I catch this database error?
« Reply #4 on: August 19, 2018, 07:10:50 pm »
The exception is already raised,but the ide doesnt handle it. So the debugging will not stop on EDATABASEERROR's if you run the debugger. This is a different meaning.
regards
Andreas

Landslyde

  • Full Member
  • ***
  • Posts: 182
  • Jesus lives!
Re: How do I catch this database error?
« Reply #5 on: August 19, 2018, 07:22:33 pm »
I'd imagine what you just told me, af0815, is written somewhere, and I shld get a copy of it. Very useful information! Thank you  :D
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.

lucamar

  • Hero Member
  • *****
  • Posts: 1982
Re: [SOLVED] How do I catch this database error?
« Reply #6 on: August 19, 2018, 08:18:23 pm »
[...] ingredient_id field is empty, so it's throwing an error. And all I want to do is quietly Exit.

IMHO, a bigger problem with your code is that you are in fact *ignoring* all possible database errors just to catch up on an easily evitable condition. Wouldn't it be better to test whether ingredient_id is empty *before* using it?

By no means a fully developed example, but something to think about:
Code: Pascal  [Select]
  1. procedure TForm1.Button_recipeIngredientsDeleteClick(Sender: TObject);
  2.  
  3.   function ValidIngredient: Boolean;
  4.   begin
  5.     {
  6.     Result := (ingredient_id <> empty) and
  7.               (whichever other check you wish to perform);
  8.     }
  9.   end;
  10.  
  11. begin
  12.   if ValidIngredient then begin
  13.     ShowMessage('Begin deletion query');
  14.     with Query_ingredients do begin { This "with" is justified, isn't it? }
  15.       SQL.Text := 'DELETE FROM recipe_ingredients WHERE ingredient_id = :id';
  16.       ParamByName('id').AsInteger := FieldByName('ingredient_id').AsInteger;
  17.       ExecSQL;
  18.     end;
  19.     dm1.Trans1.CommitRetaining;
  20.     ShowMessage('End deletion query');
  21.     Button_ingredientsRefreshClick(Sender);
  22.   end;
  23. end;
  24.  
« Last Edit: August 19, 2018, 08:25:14 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Landslyde

  • Full Member
  • ***
  • Posts: 182
  • Jesus lives!
Re: [SOLVED] How do I catch this database error?
« Reply #7 on: August 19, 2018, 09:14:26 pm »
Nice example. Thank you. However, I was testing ingredient_id last night. That's why you don't see it in my post.
Code: Pascal  [Select]
  1. if (Query_ingredients.FieldByName('ingredient_id').AsString = '') then Exit;

Giving the same error. For now, the try/except works okay. I'll go back to this bit of code, another time, and search out the tangle. There are a lot of moving parts under the hood already, so I'll chase this out before it gets much larger.

Thanks again for your time. I'll implement with like I was showing off a new truck...one without mud on it  :)
Linux Mint 19.1
Windows 10 Pro
Lazarus 2.0.4 / FPC 3.0.4


"Let he who is without sin cast the first stone." And with those words, Christ Jesus leveled the field.