Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is DX::ThrowIfFailed?

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!

like image 215
Serguei Fedorov Avatar asked Nov 27 '12 20:11

Serguei Fedorov


1 Answers

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

like image 173
James McNellis Avatar answered Oct 09 '22 16:10

James McNellis