我正在研究一个简单的类来管理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()
的失败怎么传达给被调用者。
作为一名C#程序员,这是有道理的。 – ChaosPandion 2010-02-09 01:22:51
@ChaosPandion:C#讨厌像析C++一样从析构函数抛出异常吗?我不确定... – 2010-02-09 01:24:31
为什么还要抛出异常?如果'RegCloseKey'失败会发生什么,你会忽略它? – 2010-02-09 01:27:31