{"record":{"id":"773657484e7df31f","repo":"ramensoftware/windhawk","slug":"throw-resultexception-failure","errorCode":null,"errorMessage":"throw ResultException(failure)","messagePattern":"throw ResultException\\(failure\\)","errorType":"exception","errorClass":"ResultException","httpStatus":null,"severity":"error","filePath":"src/windhawk/shared/libraries/wil/result_macros.h","lineNumber":3931,"sourceCode":"#if !defined(RESULT_SUPPRESS_STATIC_INITIALIZERS)\n    WI_HEADER_INITIALIZATION_FUNCTION(InitializeWinRt, [] {\n        g_pfnResultFromCaughtException_WinRt = ResultFromCaughtException_WinRt;\n        g_pfnResultFromKnownExceptions_WinRt = ResultFromKnownExceptions_WinRt;\n        g_pfnThrowPlatformException = ThrowPlatformException;\n        return 1;\n    });\n#endif\n#endif\n    // clang-format on\n\n    inline void __stdcall Rethrow()\n    {\n        throw;\n    }\n\n    inline void __stdcall ThrowResultExceptionInternal(const FailureInfo& failure)\n    {\n        throw ResultException(failure);\n    }\n\n    __declspec(noinline) inline ResultStatus __stdcall ResultFromCaughtExceptionInternal(\n        _Out_writes_opt_(debugStringChars) PWSTR debugString,\n        _When_(debugString != nullptr, _Pre_satisfies_(debugStringChars > 0)) size_t debugStringChars,\n        _Out_ bool* isNormalized) WI_NOEXCEPT\n    {\n        if (debugString)\n        {\n            *debugString = L'\\0';\n        }\n        *isNormalized = false;\n\n        if (details::g_pfnResultFromCaughtException_CppWinRt != nullptr)\n        {\n            const auto hr = details::g_pfnResultFromCaughtException_CppWinRt(debugString, debugStringChars, isNormalized);\n            if (FAILED(hr))\n            {","sourceCodeStart":3913,"sourceCodeEnd":3949,"githubUrl":"https://github.com/ramensoftware/windhawk/blob/61d99ed8e182e1af1b60109612b6763ad1b4b74e/src/windhawk/shared/libraries/wil/result_macros.h#L3913-L3949","documentation":"ThrowResultExceptionInternal is WIL's (Windows Implementation Library) mechanism for converting a failure HRESULT captured in a FailureInfo into a C++ ResultException. When a WIL macro (RETURN_IF_FAILED, THROW_IF_FAILED, etc.) observes a failing HRESULT, WIL's failure machinery lands here and throws ResultException(failure), carrying the HRESULT, message, and origin. It is how WIL surfaces any Windows API failure as a catchable C++ exception.","triggerScenarios":"Any WI enforcement macro in fast-fail/exception mode hitting a failure: THROW_IF_FAILED(...) / RETURN_IF_FAILED(...) wrapping a Win32 or COM call that returned a failure HRESULT, or wil::ResultException rethrown by ResultFromCaughtExceptionInternal when a caught exception is re-normalized.","commonSituations":"A Win32/COM API fails at runtime (access denied, file not found, RPC failure) inside code built with WIL result macros and WIL_ENABLE_EXCEPTIONS; callers without a try/catch around the WI macro see an unhandled ResultException.","solutions":["Read the embedded FailureInfo (hr, message, origin file/line) from the ResultException to identify the failing API call.","Fix the underlying Win32/COM failure the HRESULT reports (permissions, path, handle validity, COM registration).","Wrap WI-macro-heavy code in try/catch (const wil::ResultException&) or switch those call sites to RETURN_*/CHECK_* fail-fast variants if exceptions are unwanted.","Verify debug-string/telemetry capture (wil::SetResultLoggingCallback) so failures are visible with full context."],"exampleFix":"// before\nTHROW_IF_FAILED(OpenSomeResource(&handle));\n// after\ntry\n{\n    THROW_IF_FAILED(OpenSomeResource(&handle));\n}\ncatch (const wil::ResultException& e)\n{\n    LOG(HRESULT_FROM_WIN32(e.GetFailureInfo().hr));\n    return e.GetFailureInfo().hr;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    /* WIL macro calls */\n}\ncatch (const wil::ResultException& e)\n{\n    const auto& info = e.GetFailureInfo();\n    LOG(\"WIL failure hr=0x%08X at %s:%u\", info.hr, info.pszFile, info.uLineNumber);\n}","preventionTips":["Check return values/preconditions (ACLs, paths, handle validity) before Win32/COM calls wrapped in THROW_/RETURN_ macros.","Use wil::SetResultLoggingCallback so failures are logged with full FailureInfo context.","Prefer fail-fast (RETURN_*/WI_VERIFY) variants in code paths where exceptions cannot be handled."],"tags":["cpp","windows","wil","hresult","exception"],"backgroundTag":"wil-result-exception","analyzedSha":"61d99ed8e182e1af1b60109612b6763ad1b4b74e","analyzedAt":"2026-09-12T14:02:41.115Z","contentChangedAt":"2026-09-12T14:02:41.115Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}