c++ - 一个离开析构函数的有效异常案例

标签 c++ exception destructor

我正在开发一个简单的类来管理 HKEY 的生命周期。

class Key
{
    HKEY hWin32;
public:
    Key(HKEY root, const std::wstring& subKey, REGSAM samDesired);
    Key(const Key& other);
    ~Key();
    Key& operator=(const Key& other);
    Key& swap(Key& other);
    HKEY getRawHandle() { return hWin32; };
};

 //Other Methods....

Key::~Key()
{
    LONG errorCheck
        = RegCloseKey(hWin32);
    /*
     * I know it's generally bad to allow exceptions to leave destructors,
     * but I feel that if RegCloseKey() is going to fail, the application
     * should be terminated. (Because it should never fail.)
     */
    if (errorCheck != ERROR_SUCCESS)
        WindowsApiException::Throw(errorCheck);
}

这是有效的推理吗?我不知道如何将 RegCloseKey() 的失败传达给被调用者。

最佳答案

RegCloseKey 的失败更像是一种断言 情况,而不是需要向上传递到调用链的错误。您想在调试版本中立即坐下来注意

但是失败信息对调用者有什么用呢?他该怎么办?

关于c++ - 一个离开析构函数的有效异常案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2226118/

相关文章:

python - 如何访问 SWIG 中声明的模板结构变量?

c++ - gdb,连接到使用 gdbserver 启动的正在运行的进程

c++ - 为什么异常不退出程序?

c++ - 在 MVS 2010 的 C++ dll 中 try catch block

c++ - std::map<struct, int> 我需要析构函数吗?

c++ - 编译器对析构函数省略的自由度是多少?

c++ - 在 std::array 上使用 std::extent

c++ - 用户定义的默认构造函数是否效率较低?

c# - 如何在 .NET Core 中使用结构化参数记录异常

c++ - delete[] 操作如何在析构函数成功时崩溃? (在 C++ 中)