Search code examples
delphierror-handlingdelphi-7try-exceptuncaught-exception

Why is Try/Except statement not catching EConvertError error in Delphi?


I have a program that simulates dice rolls and compares them to values in a chart (set of String lists). I currently get the value from a TEdit. If the box is empty, it raises an EConvertError that should be caught by my Try/Except statement, but it's not.

Thoughts and advice?

The program code below is written using the Delphi programming language:

try
  //Shooting
  if ShootingRadio.Checked then
    BS := StrToInt(Edit1.Text);
  Randomize;
  Roll := RandomRange(1,7);
  Label3.Caption := IntToStr(Roll);
  if (Roll < StrToInt(ShootingHitChart[BS-1])) then
  begin
    Label3.Caption := (IntToStr(Roll)+' Miss');
    RichView1.AddTextNL((IntToStr(Roll)+' Miss'),7,0,1);
    RichView1.Reformat;
  end
  else
  begin
    Label3.Caption := (IntToStr(Roll)+' Hit');
    RichView1.AddTextNL((IntToStr(Roll)+' Hit'),6,0,1);
    RichView1.Reformat;
  end;
except
    MessageBox(0,'No number entered.','Error',mb_OK);
end;

Solution

  • 'Stop on Delphi exceptions' is checked in the debugger options. The exception is actually caught just fine, but the IDE stops when you get it. When you continue running, you will not see the exception, but your message instead. Out of the IDE it will run fine.

    You can uncheck this option (I usually do). You can always re-check it when you need to debug some stubborn problem.