Reputation: 734
I'm following the rastertek tutorial on DirectX while also studying Exception-Safety in Generic Components by David Abrahams (http://www.boost.org/community/exception_safety.html).
Why (to the best of anyone's knowledge) is the exception handling set up the way it is in the rastertek tutorial and what level of protection does it offer?
For instance:
mhresult = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &mfeatureLevel, 1,
D3D11_SDK_VERSION, &mswapChainDesc, &mswapChain, &mdevice, NULL, &mdeviceContext);
if (FAILED(mhresult))
{
return false;
}
if an unhandled exception occurs in the call to CreateDevice won't the program crash before we check the result of mhresult?
Does it make a difference whether we're calling a method with an HResult return value or just a boolean?
result = mEstablishHW();
if (!result)
{
return false;
}
Is there an alternative approach that would provide strong exception safety without a performance impact?
Upvotes: 0
Views: 3039
Reputation: 13003
DirectX is a COM-based API. And exceptions are not allowed to cross COM boundary. So no DirectX function exists that ever throw. Instead, they use C-style return codes, which are called HRESULT, to indicate errors.
To effectively work with DirectX, obviously you should learn at least some basics of COM. But here are tips to start with, to make your initial code safer and to simplify debugging:
if/else
each time. Write a macro that will check HRESULT
, break a debugger in case of "not S_OK
", and tell you a file, line and function where it happened. You could also convert HRESULT
to readable string and output it (in console).NULL
"): mswapChain
, mdevice
, mdeviceContext
in your example.Anyway, DirectX is not the API to start to learn exception safety. Maybe Standard library (like trying to write proper swap
function for your classes) or Boost (like filesystem) will work better for that purpose.
Upvotes: 7
Reputation: 39591
DirectX doesn't use exceptions to report errors or anything else. The DirectX interfaces are meant to be compiler agnostic and using C++ exceptions would tie their use to a specific compiler because of ABI differences. Even different versions Microsoft C++ would be incompatible because of ABI differences.
Since most DirectX methods and functions return an HRESULT
value, you'll want to check for errors like in your first example. You can then throw an exception in your own code if that makes sense for your project.
Upvotes: 0