c++ - MFC TRY CATCH 与 C++ try catch for MFC 异常

标签 c++ mfc

我使用普通的 C++ trycatch 来处理异常。现在我想学习如何使用MFC TRY CATCH。我尝试使用 C++ 语法和 MFC 关键字抛出一个 int 变量,如下所示:

...
var = 0;
    TRY
    {
        if (var == 0)
        {
            THROW 5;
        }
    }
    CATCH(int a)
    {
      MessageBoxW(L"Blub", L"blub", NULL);
    }
    END_CATCH

但是它无法编译。

问题出在哪里?谁能给我解释一下?

或者发送一个很好的链接,我可以在其中学习 MFC TRY CATCH 方法。我一直找不到很好的解释。

最佳答案

来自 MSDN Exceptions: Converting from MFC Exception Macros

You probably do not need to convert existing code, although you should be aware of differences between the macro implementations in MFC version 3.0 and the implementations in earlier versions. These differences and subsequent changes in code behavior are discussed in Exceptions: Changes to Exception Macros in Version 3.0. The principal advantages of converting are:

  • Code that uses the C++ exception-handling keywords compiles to a slightly smaller .EXE or .DLL.

  • The C++ exception-handling keywords are more versatile: They can handle exceptions of any data type that can be copied (int, float, char, and so on), whereas the macros handle exceptions only of class CException and classes derived from it.

The major difference between the macros and the keywords is that code using the macros "automatically" deletes a caught exception when the exception goes out of scope. Code using the keywords does not, so you must explicitly delete a caught exception. For more information, see the article Exceptions: Catching and Deleting Exceptions.

MFC 只能捕获CException 或派生类,如果要处理其他数据类型,请使用c++ 异常。

关于c++ - MFC TRY CATCH 与 C++ try catch for MFC 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26829518/

相关文章:

c++ - 你能禁止类的本地实例化吗?

c++ - 两个函数变量到一个函数

c++ - 如何决定堆栈中的内容?

c# - 如何获得鼠标滚轮垂直滚动率

c++ - MFC MessageMap 和虚函数

c++ - 为特定的 mfc dll 调用 AfxEnableMemoryLeakDump

c++ - 无法识别子进程中的 Googletest Explicit FAIL()

c++ - 在 C++ 中使用逻辑作为参数

c++ - 如何使用 Microsoft 链接器工具链接静态 MFC 库

visual-c++ - MFC:更改CEdit的颜色