尽管异常包含在 catch 语句中,但未捕获异常

发布于 2024-12-24 19:53:35 字数 625 浏览 3 评论 0原文

我用 C++ Builder 6 编写了这个程序。我没有编写所有代码,只编写了其中的一些代码。然而,该语言不是 C++(据我所知)——它看起来更像 Delphi 或 Pascal。这就是为什么我将它们全部包含在标签中。

我有一个名为Oversteering的int。

try
{
    Oversteering=HoursCounter.ToInt();
}
catch(EConvertError &convertError)
{
    Oversteering=0;
}

HoursCounter 是一个 AnsiString,它的形式是 int。

由于这是整个代码中唯一的 try/catch 语句(我知道这不太好),而且我在 Delphi/Pascal/??? 中找不到任何好的例子,我不知道它是否是正确书写。

好吧,我尝试将字符串转换为 int。有时我会收到此错误:

error

也就是说,发生了名为 EConvertError 的异常。

所以我的问题是:为什么catch语句没有捕获这个异常?

I have this program written in C++ Builder 6. I didn't write all the code, just some of it. The language, however, is not C++ (as far as I'm aware) - it looks more like Delphi or Pascal. So that's why I included them all in the tags.

I have an int called Oversteering.

try
{
    Oversteering=HoursCounter.ToInt();
}
catch(EConvertError &convertError)
{
    Oversteering=0;
}

HoursCounter is an AnsiString, and it is in the form of an int.

Since this is the only try/catch statement in the whole code (that's not too good, I know), and I couldn't find any good example of such in Delphi/Pascal/???, I don't know if it's correctly written.

Well, I try to convert the string to an int. Sometimes I get this error:

error

That is, an exception called EConvertError has occurred.

So my question is: why is this exception NOT caught by the catch statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-12-31 19:53:35

调试器在运行代码时会显示此错误,
如果您运行 exe 并遇到相同的情况,则不会向您显示错误消息

捕获异常,但调试器会通知您有关

出现的

  try
     {
      Oversteering=HoursCounter.ToInt();
     }

在调试器中运行后 代码中的错误,您尝试将(空白)'' 转换为整数,调试器将显示异常...但是在运行 exe 时,调试器将设置

       Oversteering=0

about.com

中断异常
当构建带有异常处理的程序时,您可能不希望 Delphi 在异常时中断。如果您希望 Delphi 显示发生异常的位置,那么这是一个很棒的功能;但是,当您测试自己的异常处理时,这可能会很烦人。

This error is shown by the debugger when running through the code,
if you run the exe and have the same situation the error message will not be shown to you

The exception is caught but the debugger is notifiying you regarding the error in the code

that is here

  try
     {
      Oversteering=HoursCounter.ToInt();
     }

since running in the debugger the ,your trying to convert (blankspace) '' to integer, the debugger will show the exception...but when running the exe, the debugger will set

       Oversteering=0

check this from about.com

Break On Exceptions
When building a program with exception handling, you may not want Delphi to break on Exceptions. This is a great feature if you want Delphi to show where an exception has occurred; however, it can be annoying when you test your own exception handling.

裂开嘴轻声笑有多痛 2024-12-31 19:53:35

正如 @PresleyDias 所解释的,是调试器显示异常,而不是您的应用程序。异常正在被捕获(不过,您应该通过 const 引用来捕获它),但调试器会在您的应用程序之前看到它,仅此而已。如果您愿意,可以将调试器配置为忽略 EConvertError

更好的解决方案是首先避免异常。如果您使用 AnsiString::ToIntDef() 代替,则可以完全删除 try/catch 块:

Oversteering = HoursCounter.ToIntDef(0); 

或者,您可以使用 TryStrToInt()相反:

if (!TryStrToInt(HoursCounter, Oversteering))
{
    ...;
}

如果 0 是计数器的有效值,请使用 TryStrToInt()

if (TryStrToInt(HoursCounter, Oversteering))
{
    // use Oversteering as needed, even zeros...
}
else
    ShowMessage("Cannot convert HoursCounter to a valid integer!");

如果 0 始终表示错误,则使用ToIntDef()

Oversteering = HoursCounter.ToIntDef(0);
if (Oversteering != 0)
{
    // use Oversteering as needed, except zeros...
}
else
    ShowMessage("Cannot convert HoursCounter to an acceptable integer!");

As @PresleyDias explained, it is the debugger that is displaying the exception, not your app. The exception is being caught (you should be catching it by a const reference, though), but the debugger sees it before your app does, that's all. You can configure the debugger to ignore EConvertError, if you like.

A better solution is to avoid the exception in the first place. If you use AnsiString::ToIntDef() instead, you can remove the try/catch block completely:

Oversteering = HoursCounter.ToIntDef(0); 

Alternatively, you can use TryStrToInt() instead:

if (!TryStrToInt(HoursCounter, Oversteering))
{
    ...;
}

If 0 is a valid value for your counter, use TryStrToInt():

if (TryStrToInt(HoursCounter, Oversteering))
{
    // use Oversteering as needed, even zeros...
}
else
    ShowMessage("Cannot convert HoursCounter to a valid integer!");

If 0 always represents an error, then use ToIntDef():

Oversteering = HoursCounter.ToIntDef(0);
if (Oversteering != 0)
{
    // use Oversteering as needed, except zeros...
}
else
    ShowMessage("Cannot convert HoursCounter to an acceptable integer!");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文