Hresult Throw

Posted on by admin
-->

A helper function that takes a HRESULT error code, and throws an exception using a C++/WinRT object (or a standard object) that represents that error code.

If the error code is E_OUTOFMEMORY, then std::bad_alloc is thrown. If the error code is a common HRESULT error code, then one of the specialized types derived from winrt::hresult_error is thrown. For example, E_INVALIDARG causes a winrt::hresult_invalid_argument to be thrown. Otherwise, winrt::hresult_error is thrown.

Hresult throw
  1. Throw an exception only when an unexpected runtime error occurs, and handle everything else with error/result codes—directly, and close to the source of the failure. That way, when an exception is thrown, you know that the cause is either a bug in your code, or an exceptional error state in the system.
  2. Are you sure that the exception is not occurring in the call to pMapDoc.Open, because that method will throw STGEFILENOTFOUND if the MXD path is invalid? – Adam Armour Jun 3 '11 at 13:54 @Kirk: The MXD in question is currently pointing to client-side database instance, which to us developers is not accessible.

The class described in this article just makes it easy to throw an exception every time a failure is returned from another function. This way you can do only what you have to do in the code and unify error handling.

Hresult Thrown

Syntax

Parameters

C++ Throw Hresult

resultAn HRESULT code that represents the error that was encountered.

Hresult Throwing

ThrowHresult Throw

Requirements

Minimum supported SDK: Windows SDK version 10.0.17134.0 (Windows 10, version 1803)

Namespace: winrt

Header %WindowsSdkDir%Include<WindowsTargetPlatformVersion>cppwinrtwinrtbase.h (included by default)

C# Throw Hresult

See also