I'd like to encapsulate Win32 errors (those returned from GetLastError()) in some form of exception class. Rather than having a single Win32 exception, however, I'd like to be able to have a specialized exception catchable for common errors, such as ERROR_ACCESS_DENIED.
For example, I'd have classes declared like this:
class WindowsException开发者_开发技巧 : public std::exception
{
public:
    static WindowsException Create(DWORD lastError);
    //blah
};
class ErrorAccessDeniedException : public WindowsException
{
public:
    //blah
};
However, I'd like the Win32 exception to be responsible for picking the right exception to return. That is, the thrower of the exception should look like:
int DangerousMethod() {
    throw WindowsAPI::WindowsException::Create(GetLastError());
}
and the catcher might look like:
try
{
    DangerousMethod();
} catch(WindowsAPI::ErrorAccessDeniedException ex)
{
    //Code for handling ERROR_ACCESS_DENIED
} catch(WindowsAPI::WindowsException ex)
{
    //Code for handling other kinds of error cases.
}
My problem is that if the WindowsException::Create factory method returns a WindowsException, then the subtype (potentially ErrorAccessDeniedException) is sliced down to the base type. That is, the instance can't be polymorphic. I don't want to use a new'd pointer, because that would force the exception handler to delete it when it's done.
Does anyone know of a design solution that would be feasible for solving this problem elegantly?
Billy3
Change
int DangerousMethod() {
    throw WindowsAPI::WindowsException::Create(GetLastError());
}
To
int DangerousMethod() {
    WindowsAPI::WindowsException::Throw(GetLastError());
}
Meaning, instead of returning the exception then throwing it (which will slice, as you observed), have your helper/factory method throw it directly.
Some more exception handling background reading: http://www.informit.com/articles/article.aspx?p=373339
A note on type slicing and rethrowing:
When rethrowing an exception e , prefer writing just throw; instead of throw e; because the first form always preserves polymorphism of the rethrown object.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论