-
Notifications
You must be signed in to change notification settings - Fork 513
ThrowIfFailed
When programming COM APIs like Direct3D, it is important to always check the HRESULT
return value for success or failure. This can be done using the SUCCEEDED
or FAILED
macros, but can get tedious when making lots of calls
Not all Direct3D functions return
HRESULT
. Many of them returnvoid
because they can't fail, fail silently, or the failure will be reported on the nextPresent
.
For "modern" Direct3D programming, the recommended solution is to throw a C++ exception on a failed HRESULT
. The C++ DirectX templates for universal Windows apps, Windows 8 Store, Windows phone 8, Xbox One, and the Direct3D Win32 Game templates all make use of the DX::ThrowIfFailed
helper.
#include <exception>
namespace DX
{
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
// Set a breakpoint on this line to catch DirectX API errors
throw std::exception();
}
}
}
The usage is very simple.
DX::ThrowIfFailed( device->CreateTexture2D(&depthStencilDesc,
nullptr, &depthStencil) );
DX::ThrowIfFailed
should be used whenever a failure is fatal and should result in 'fast-fail' of the application. Otherwise, traditional if FAILED(hr)
or if SUCCEEDED(hr)
patterns should be used to handle failures that the application can recover from (i.e. are not fatal).
The legacy DXUT framework makes use of macros like
V
andV_RETURN
as a pattern for dealing withHRESULT
values, but these make assumptions about the surrounding functions.
The templates all include the basic implementation above, but production use might want to utilize a slightly improved version as follows:
#include <exception>
namespace DX
{
// Helper class for COM exceptions
class com_exception : public std::exception
{
public:
com_exception(HRESULT hr) : result(hr) {}
virtual const char* what() const override
{
static char s_str[64] = { 0 };
sprintf_s(s_str, "Failure with HRESULT of %08X", result);
return s_str;
}
private:
HRESULT result;
};
// Helper utility converts D3D API failures into exceptions.
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
throw com_exception(hr);
}
}
}
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
- Universal Windows Platform apps
- Windows desktop apps
- Windows 11
- Windows 10
- Windows 8.1
- Xbox One
- x86
- x64
- ARM64
- Visual Studio 2022
- Visual Studio 2019 (16.11)
- clang/LLVM v12 - v18
- MinGW 12.2, 13.2
- CMake 3.20