Hresult Throw

Hresult Throw Rating: 8,1/10 3187 votes
-->

This topic discusses strategies for handling errors when programming with C++/WinRT. For more general info, and background, see Errors and Exception Handling (Modern C++).

Avoid catching and throwing exceptions

Hello Chris, As you are using version 7.11, note that the robustness improvements previously mentioned here were included in a hotfix (or actually two - one for the client (TIBCO Spotfire Analyst), and one for the server (TIBCO Spotfire Server)) for that release so I would recommend that you first confirm that you have hotfix HF-004 or higher for both. If the child excel is opened by one excel scope and then if we try to open and perform activities on the master excel (Even after few seconds delay), it throws this exception. The fix in this case was to close the child excel after performing any operations and then open up the master excel. When managed code throws an exception, the runtime passes the HRESULT to the COM client. When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime. For information about HRESULT values and their corresponding.NET Framework exceptions, see How to: Map HRESULTs and Exceptions.

We recommend that you continue to write exception-safe code, but that you prefer to avoid catching and throwing exceptions whenever possible. If there's no handler for an exception, then Windows automatically generates an error report (including a minidump of the crash), which will help you track down where the problem is.

Don't throw an exception that you expect to catch. And don't use exceptions for expected failures. 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.

Consider the scenario of accessing the Windows Registry. If your app fails to read a value from the Registry, then that's to be expected, and you should handle it gracefully. Don't throw an exception; rather return a bool or enum value indicating that, and perhaps why, the value wasn't read. Failing to write a value to the Registry, on the other hand, is likely to indicate that there's a bigger problem than you can handle sensibly in your application. In a case like that, you don't want your application to continue, so an exception that results in an error report is the fastest way to keep your application from causing any harm.

Hresult throw exception

For another example, consider retrieving a thumbnail image from a call to StorageFile.GetThumbnailAsync, and then passing that thumbnail to BitmapSource.SetSourceAsync. If that sequence of calls causes you to pass nullptr to SetSourceAsync (the image file can't be read; perhaps its file extension makes it look like it contains image data, but it actually doesn't), then you'll cause an invalid pointer exception to be thrown. If you discover a case like that in your code, rather than catching and handling the case as an exception, instead check for nullptr returned from GetThumbnailAsync.

Throwing exceptions tends to be slower than using error codes. If you only throw an exception when a fatal error occurs, then if all goes well you'll never pay the performance price.

But a more likely performance hit involves the runtime overhead of ensuring that the appropriate destructors are called in the unlikely event that an exception is thrown. The cost of this assurance comes whether an exception is actually thrown or not. So, you should ensure that the compiler has a good idea of what functions can potentially throw exceptions. If the compiler can prove that there won't be any exceptions from certain functions (the noexcept specification), then it can optimize the code it generates.

Catching exceptions

An error condition that arises at the Windows Runtime ABI layer is returned in the form of a HRESULT value. But you don't need to handle HRESULTs in your code. The C++/WinRT projection code that's generated for an API on the consuming side detects an error HRESULT code at the ABI layer and converts the code into a winrt::hresult_error exception, which you can catch and handle. If you do wish to handle HRESULTS, then use the winrt::hresult type.

For example, if the user happens to delete an image from the Pictures Library while your application is iterating over that collection, then the projection throws an exception. And this is a case where you'll have to catch and handle that exception. Here's a code example showing this case.

Use this same pattern in a coroutine when calling a co_await-ed function. Another example of this HRESULT-to-exception conversion is that when a component API returns E_OUTOFMEMORY, that causes a std::bad_alloc to be thrown.

Prefer winrt::hresult_error::code when you're just peeking at a HRESULT code. The winrt::hresult_error::to_abi function on the other hand converts to a COM error object, and pushes state into the COM thread-local storage.

Throwing exceptions

There will be cases where you decide that, should your call to a given function fail, your application won't be able to recover (you'll no longer be able to rely on it to function predictably). The code example below uses a winrt::handle value as a wrapper around the HANDLE returned from CreateEvent. It then passes the handle (creating a bool value from it) to the winrt::check_bool function template. winrt::check_bool works with a bool, or with any value that's convertible to false (an error condition), or true (a success condition).

If the value that you pass to winrt::check_bool is false, then the following sequence of actions take place.

  • winrt::check_bool calls the winrt::throw_last_error function.
  • winrt::throw_last_error calls GetLastError to retrieve the calling thread's last-error code value, and then calls the winrt::throw_hresult function.
  • winrt::throw_hresult throws an exception using a winrt::hresult_error object (or a standard object) that represents that error code.
C# throw hresult

Because Windows APIs report run-time errors using various return-value types, there are in addition to winrt::check_bool a handful of other useful helper functions for checking values and throwing exceptions.

  • winrt::check_hresult. Checks whether the HRESULT code represents an error and, if so, calls winrt::throw_hresult.
  • winrt::check_nt. Checks whether a code represents an error and, if so, calls winrt::throw_hresult.
  • winrt::check_pointer. Checks whether a pointer is null and, if so, calls winrt::throw_last_error.
  • winrt::check_win32. Checks whether a code represents an error and, if so, calls winrt::throw_hresult.

You can use these helper functions for common return code types, or you can respond to any error condition and call either winrt::throw_last_error or winrt::throw_hresult.

Throwing exceptions when authoring an API

All Windows Runtime Application Binary Interface boundaries (or ABI boundaries) must be noexcept—meaning that exceptions must never escape there. When you author an API, you should always mark the ABI boundary with the C++ noexcept keyword. noexcept has specific behavior in C++. If a C++ exception hits a noexcept boundary, then the process will fail fast with std::terminate. That behavior is generally desirable, because an unhandled exception almost always implies unknown state in the process.

Since exceptions mustn't cross the ABI boundary, an error condition that arises in an implementation is returned across the ABI layer in the form of an HRESULT error code. When you're authoring an API using C++/WinRT, code is generated for you to convert any exception that you do throw in your implementation into an HRESULT. The winrt::to_hresult function is used in that generated code in a pattern like this.

C++ Throw Hresult

winrt::to_hresult handles exceptions derived from std::exception, and winrt::hresult_error and its derived types. In your implementation, you should prefer winrt::hresult_error, or a derived type, so that consumers of your API receive rich error information. std::exception (which maps to E_FAIL) is supported in case exceptions arise from your use of the Standard Template Library.

Debuggability with noexcept

As we mentioned above, a C++ exception hitting a noexcept boundary fails fast with std::terminate. That's not ideal for debugging, because std::terminate often loses much or all of the error or the exception context thrown, especially when coroutines are involved.

So, this section deals with the case where your ABI method (which you've properly annotated with noexcept) uses co_await to call asynchronous C++/WinRT projection code. We recommend that you wrap the calls to the C++/WinRT projection code within a winrt::fire_and_forget. Doing so provides a proper place for an unhandled exception to be properly recorded as a stowed exception, which greatly increases debuggability.

winrt::fire_and_forget has a built-in unhandled_exception method helper, which calls winrt::terminate, which in turn calls RoFailFastWithErrorContext. This guarantees that any context (stowed exception, error code, error message, stack backtrace, and so on) is preserved either for live debugging or for a post-mortem dump. For convenience, you can factor the fire-and-forget portion into a separate function that returns a winrt::fire_and_forget, and then call that.

Synchronous code

In some cases, your ABI method (which, again, you've properly annotated with noexcept) calls only synchronous code. In other words, it never uses co_await, either to call an asynchronous Windows Runtime method, or to switch between foreground and background threads. In that case, the fire_and_forget technique will still work, but it's not efficient. Instead, you can do something like this.

Fail fast

The code in the previous section still fails fast. As written, that code doesn't handle any exceptions. Any unhandled exception results in program termination.

But that form is superior, because it ensures debuggability. In rare cases, you might want to try/catch, and handle certain exceptions. But that should be rare because, as this topic explains, we discourage using exceptions as a flow-control mechanism for conditions that you expect.

Hresult Throwing

Remember that it's a bad idea to let an unhandled exception escape a naked noexcept context. Under that condition, the C++ runtime will std::terminate the process, thereby losing any stowed exception information that C++/WinRT carefully recorded.

Assertions

For internal assumptions in your application, there are assertions. Prefer static_assert for compile-time validation, wherever possible. For run-time conditions, use WINRT_ASSERT with a Boolean expression. WINRT_ASSERT is a macro definition, and it expands to _ASSERTE.

WINRT_ASSERT is compiled away in release builds; in a debug build, it stops the application in the debugger on the line of code where the assertion is.

HresultHresult Throw

You shouldn't use exceptions in your destructors. So, at least in debug builds, you can assert the result of calling a function from a destructor with WINRT_VERIFY (with a Boolean expression) and WINRT_VERIFY_ (with an expected result and a Boolean expression).

Throw Hresult

C# throw hresult

Important APIs

Hresult Thrown

Related topics