[SOLVED] How do I catch this database error? lazarus / 16-Databases ---------------------------------------------------------------------- guest48180 · 2018-08-19 16:23 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.Button_recipeIngredientsDeleteClick(Sender: TObject);begin  try      ShowMessage('Begin deletion query');                  Query_ingredients.SQL.Text := 'DELETE FROM recipe_ingredients WHERE ingredient_id = :id';                  Query_ingredients.ParamByName('id').AsInteger := Query_ingredients.FieldByName('ingredient_id').AsInteger;                Query_ingredients.ExecSQL;                  dm1.Trans1.CommitRetaining;      ShowMessage('End deletion query');           Button_ingredientsRefreshClick(Sender);  except    On e: EDatabaseError do begin       Exit;     end;  end;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? ---------------------------------------------------------------------- rvk · 2018-08-19 16:28 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. ---------------------------------------------------------------------- guest48180 · 2018-08-19 16:42 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? ---------------------------------------------------------------------- guest48180 · 2018-08-19 16:55 Quote from: rvk on August 19, 2018, 06:28:26 pmAre 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. ---------------------------------------------------------------------- af0815 · 2018-08-19 17:10 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. ---------------------------------------------------------------------- guest48180 · 2018-08-19 17:22 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 ---------------------------------------------------------------------- lucamar · 2018-08-19 18:18 Quote from: Landslyde on August 19, 2018, 06:23:45 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button_recipeIngredientsDeleteClick(Sender: TObject);   function ValidIngredient: Boolean;  begin    {    Result := (ingredient_id <> empty) and              (whichever other check you wish to perform);    }  end; begin  if ValidIngredient then begin    ShowMessage('Begin deletion query');    with Query_ingredients do begin { This "with" is justified, isn't it? }      SQL.Text := 'DELETE FROM recipe_ingredients WHERE ingredient_id = :id';      ParamByName('id').AsInteger := FieldByName('ingredient_id').AsInteger;      ExecSQL;    end;    dm1.Trans1.CommitRetaining;    ShowMessage('End deletion query');    Button_ingredientsRefreshClick(Sender);  end;end; ---------------------------------------------------------------------- guest48180 · 2018-08-19 19:14 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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