gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:32:05 25 4
gpt4 key购买 nike

我正在开发一个简单的类来管理 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com