6

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 a EConvertError that should be caught by my Try/Except statement, but it's not. Thoughts and advice? Code below, Delphi 7.

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;
menjaraz
  • 7,551
  • 4
  • 41
  • 81
Aaron
  • 896
  • 3
  • 11
  • 22
  • 2
    Does it catch it if you run it without the debugger (start the program directly in windows, not in Delphi)? – Avo Muromägi Oct 28 '11 at 13:57
  • 1
    Change StrToInt in if (ShootingRadio.Checked = True) and ( TryStrToInt(Edit1.Text, BS)) then begin ... end; – Arjen van der Spek Oct 28 '11 at 14:03
  • If an exception is raised in between the try and except then the message box will be shown. Therefore I conclude no exception is being raised. Does the new indentation help? – David Heffernan Oct 28 '11 at 14:08
  • 1
    See also: [Why do I continue getting error messages even after I have written an exception handler?](http://www.cs.wisc.edu/~rkennedy/exception-messages) – Rob Kennedy Oct 28 '11 at 14:52

1 Answers1

11

'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.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 3
    I prefer break on exception. I like to know what's going on. Code that raises a lot of exceptions, that really are used for flow control (Indy, I'm looking at you) drives me crazy. – Warren P Oct 28 '11 at 14:27
  • 1
    @WarrenP Not sure about 7, but in recent versions, you can select a list of exception types to ignore. – GolezTrol Oct 28 '11 at 14:31
  • 1
    If this is the correct answer then the question is wrong! Question says that the exception handler does not run! Ah, go figure. – David Heffernan Oct 28 '11 at 14:51
  • 1
    @DavidHeffernan Unfortunatly, it is a common misconception that putting a `try..except` block around the code would cause the IDE to ignore that exception too. I've seen this problem more than once on various occasions, and judging by the first comment to the question, so did Avo Muromägi. – GolezTrol Oct 28 '11 at 15:00
  • And unfortunately he didn't put that as an answer, which I would have accepted. – Aaron Oct 28 '11 at 15:03
  • @TOndrej No criticism of the answer! It's the question that's off. Anyway, it's good that somebody worked it out – I'm quite sure I would never have come up with this answer! ;-) – David Heffernan Oct 28 '11 at 15:06
  • You're too good at programming to even think that someone would make an error like this, that's why. :) – GolezTrol Oct 28 '11 at 15:07
  • an alternative approach to disabling "break on exception" is to use non-breaking breakpoints to enable and disable breaking - see http://edn.embarcadero.com/article/31263 for details, specifically `Ignore subsequent exceptions` and `Handle subsequent exceptions`. This is useful when you have an exception you can't prevent, e.g. is third party code. – Gerry Coll Oct 30 '11 at 23:38