Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9026175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:27:19+00:00 2026-06-16T06:27:19+00:00

What is the correct way to handle errors? I’ve code like following, handling all

  • 0

What is the correct way to handle errors? I’ve code like following, handling all error codes I’ve found on MSDN and still, sometimes, I’ve received error “Unknown”.

HRESULT hr = pwb->Navigate2(&URL, &Flag, &TargetFrameName, &PostData, &Headers);
if(FAILED(hr))
{
    std::string message("Navigate2 failed with reason: ");
    switch(hr)
    {
        // The operation was successful.
        case S_OK:
            message.append("S_OK");
            break;
        // One or more parameters are invalid.
        case E_INVALIDARG:
            message.append("E_INVALIDARG");
            break;
        // Out of memory.
        case E_OUTOFMEMORY:
            message.append("E_OUTOFMEMORY");
            break;
        // The operation failed.
        case E_FAIL:
            message.append("E_FAIL");
            break;
        case E_ACCESSDENIED:
            message.append("E_ACCESSDENIED");
            break;
        case E_POINTER:
            message.append("E_POINTER");
            break;
        case E_UNEXPECTED:
            message.append("E_UNEXPECTED");
            break;
        default:
            message.append("Unknown");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:27:20+00:00Added an answer on June 16, 2026 at 6:27 am

    There’s quite a few variations on how to handle COM errors. Strategies include use of cascading if SUCCEEDED() or centralized function error handling using goto on FAILED() among others. There’s some good information on MSDN.

    As to interpreting error codes, FormatMessage() will often make your work easier – here’s an example from MSDN (included below for clarity)

    [Source: http://msdn.microsoft.com/en-us/library/windows/desktop/ms687061(v=vs.85).aspx]

        #include <stdio.h>
        #include <windows.h>
        #include <tchar.h>
    
        void ErrorDescription(HRESULT hr) 
        { 
             if(FACILITY_WINDOWS == HRESULT_FACILITY(hr)) 
                 hr = HRESULT_CODE(hr); 
             TCHAR* szErrMsg; 
    
             if(FormatMessage( 
               FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, 
               NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
               (LPTSTR)&szErrMsg, 0, NULL) != 0) 
             { 
                 _tprintf(TEXT("%s"), szErrMsg); 
                 LocalFree(szErrMsg); 
             } else 
                 _tprintf( TEXT("[Could not find a description for error # %#x.]\n"), hr); 
        }
    

    In the call above, the flags indicate that windows will allocate memory for error messages (which you have to free – use LocalFree()) and that it will look for error messages in the system message tables (FORMAT_MESSAGE_FROM_SYSTEM). Sometimes, (or often depending on the kind of libraries you use) – no corresponding error descriptions will be found in the system message tables.

    In cases like these you may either handle the error description yourself (as you have done in your example) or attempt to load a library’s message tables directly. To do this, use the FORMAT_MESSAGE_FROM_HMODULE and supply a module handle as the lpSource parameter to the FormatMessage() function.

    Here’s an example:

    std::wstring StackExample::getLastError( HRESULT hr ) 
    {
        LPWSTR lpMsgBuf;
        DWORD ret;
        std::wstring def(L"(UNKNOWN)");
        ret = FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_HMODULE,
            GetModuleHandle(TEXT("imapi2.dll")),
            hr,
            0,
            (LPWSTR) &lpMsgBuf,
            0, NULL );
    
        if(ret)
        {
            std::wstring last(lpMsgBuf);
            LocalFree(lpMsgBuf);
            return last;
        }
        return def;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about the correct way to handle errors in VBA in
When using filesystem functions , what would be the correct way of handling errors
How would you correct way to handle an exception in this situation? Initially I
What is the correct way to handle an orientation change when using Fragments? I
I'm trying to work out the best way of handling custom error pages within
What is the correct way to add a product variant? I create the product
What's the correct way to set a flash message for the current view but
Which is correct way to extend/subclass components? Ext.define('Holidays.Components.UserInfo', { extend: 'Ext.panel.Panel', alias: 'widget.UserInfo', region:
What is the correct way to use pygame.Color names when using unicode_literals ? Python
What is the correct way to draw isometric tiles in a 2D game? I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.