为什么 Try/Except 语句无法捕获 Delphi 中的 EConvertError 错误?
我有一个程序可以模拟掷骰子并将它们与图表(一组字符串列表)中的值进行比较。我目前从 TEdit
获取值。如果该框为空,则会引发一个 EConvertError
,该错误应该由我的 Try/Except 语句捕获,但事实并非如此。
想法和建议?
下面的程序代码是使用Delphi编程语言编写的:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在调试器选项中选中“出现 Delphi 异常时停止”。实际上,异常可以很好地捕获,但是当您捕获它时 IDE 就会停止。当您继续运行时,您将不会看到异常,而是看到消息。离开 IDE 就可以正常运行了。
您可以取消选中此选项(我通常这样做)。当你需要调试一些顽固的问题时,你可以随时重新检查它。
'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.