简体   繁体   English

C ++ Builder 5中的VCL异常

[英]VCL Exceptions in C++ Builder 5

I'm trying to control exception raised in the code, however this sample is not working as it should under C++ Builder 5. 我正在尝试控制代码中引发的异常,但是此示例无法正常运行,在C ++ Builder 5中应该如此。

void __fastcall TForm1::Button1Click(TObject *Sender)
{ 
    try
    {
         throw Exception("NoNumber");
    }
    catch(Exception& E)
    {
        // but we never get the LALAL message
        ShowMessage("LALAL");
    }
}

Why is the catch block never reached when the exception is raised ? 为什么在引发异常时无法到达catch块?

First, you should catch exceptions by const reference instead: 首先,您应该改为通过const引用捕获异常:

catch(const Exception& E)

That allows the compiler to emit slightly more efficient code for managing the exception. 这样一来,编译器就可以发出更有效的代码来管理异常。 However, that alone would not prevent the exception from being caught. 但是,仅此一项并不能阻止捕获异常。

If you are running the app inside of the debugger then keep in mind that the debugger will catch the exception first, so you have to tell the debugger to pass the exception back to your app for normal handling by pressing F9 or the Run toolbar button, or else configure the debugger to ignore exceptions. 如果您在调试器内部运行应用程序,请记住调试器将首先捕获异常,因此您必须通过按F9或“运行”工具栏按钮告诉调试器将异常传递回您的应用程序以进行正常处理,否则将调试器配置为忽略异常。

If you are running the app outside of a debugger, then there is nothing wrong with the code you have shown that would prevent the catch from catching the exception under normal conditions. 如果您是在调试器之外运行应用程序,那么您显示的代码没有任何问题,可以防止catch在正常情况下捕获异常。

I used BCB5 for years and this type of code has always worked just fine for me (though I always use const ). 我使用BCB5已有多年,这种类型的代码对我来说一直很好(尽管我始终使用const )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM