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

1

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 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;
Sibley answered 28/10, 2011 at 13:46 Comment(4)
Does it catch it if you run it without the debugger (start the program directly in windows, not in Delphi)?Fingerboard
Change StrToInt in if (ShootingRadio.Checked = True) and ( TryStrToInt(Edit1.Text, BS)) then begin ... end;Spectroradiometer
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?Sinistrodextral
See also: Why do I continue getting error messages even after I have written an exception handler?Sibelle
C
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.

Conflation answered 28/10, 2011 at 14:18 Comment(8)
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.Hickman
@WarrenP Not sure about 7, but in recent versions, you can select a list of exception types to ignore.Conflation
If this is the correct answer then the question is wrong! Question says that the exception handler does not run! Ah, go figure.Sinistrodextral
@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.Conflation
And unfortunately he didn't put that as an answer, which I would have accepted.Sibley
@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! ;-)Sinistrodextral
You're too good at programming to even think that someone would make an error like this, that's why. :)Conflation
an alternative approach to disabling "break on exception" is to use non-breaking breakpoints to enable and disable breaking - see 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.Odessa

© 2022 - 2024 — McMap. All rights reserved.