Search code examples
uwpwindows-runtimec++-winrt

What is the recommended way to report errors from custom WinRT component?


I'm authoring a custom C++ WinRT component library to be consumed by third parties. I'm unable to determine what the 'best practices' are for reporting/propagating runtime error conditions from my library.

I've found this article which details best practices for handling exceptions and crossing the WinRT ABI via HRESULTs: https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/error-handling.

However I'm unclear what to do for custom (exceptional) errors. I'd like to follow modern c++ practices and throw std::runtime_errors, but the WinRT documentation says that's reserved for mapping standard library errors to the 'E_FAIL' HRESULT. Should I be following the COM model and implementing custom HRESULT values, just for the sake of throwing them?


Solution

  • Just throw a winrt::hresult_error, with hresult code and error message. You can also throw an exception inherited from it. The code generated by cppwinrt will convert your exception to HRESULT.