What is DX::ThrowIfFailed?
Asked Answered
A

1

10

I have been getting back into C++ lately. I've been away from C++/CLI using C# instead for at least a year now and I am a bit rusty. I am looking at the base example for a Direct3D app for Windows 8 and cannot find anything that explains what the

 DX::ThrowIfFailed

does. From what it says it will execute something if something in DirectX failed, but from the implementation it looks like it is being used for initializing stuff as the base for the Direct3D demonstrates:

    Platform::String^ text = "Hello, DirectX!";

DX::ThrowIfFailed(
    m_dwriteFactory->CreateTextLayout(
        message->Data(),
        message->Length(),
        m_textFormat.Get(),
        700, // maxWidth.
        1000, // maxHeight.
        &m_textLayout
        )
    );

Can someone explain to me how this function works. I see it scattared accross examples but no amount of googling has relieved proper documentation. Thank you ahead of time!

Alexia answered 27/11, 2012 at 20:13 Comment(1)
This is not C++/CLI. It is C++/CX. Very similar syntax; very different semantics.Habitable
H
11

This function translates failure HRESULTs into exceptions. It is defined like so, in DirectXHelper.h, which is part of the Direct3D App template:

inline void ThrowIfFailed(HRESULT hr)
{
    if (FAILED(hr))
    {
        // Set a breakpoint on this line to catch Win32 API errors.
        throw Platform::Exception::CreateException(hr);
    }
}

If you are using Visual Studio, you can right-click on any instance of ThrowIfFailed in the code and select "Go To Definition." This will open the file that contains the definition and navigate to its location.

For more information on this helper, see GitHub

Habitable answered 27/11, 2012 at 20:16 Comment(5)
for some reason I assumed this was part of the Direct3D stuff. Next time ill make sure to go to definition on everything before asking. Thanks a lot!Alexia
Even if it is part of Direct3D, it would have to be either in an included header file or in a referenced Windows Metadata file; in either case, Go To Definition should find the definition.Habitable
@JamesMcNellis i know this is a little bit old thread but what if i don't have DirectXHelper.h? (Windows 8.1 64bit MSVC 2013)Fechner
@Quest, the windows SDK samples have this header. you'll find those here: linkTorrez
There's also a simple implementation inline in the DIrect3D Win32 Game template.Decadence

© 2022 - 2024 — McMap. All rights reserved.