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