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 399487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:51:04+00:00 2026-05-12T16:51:04+00:00

As of the MSDN spec, CloseHandle throws an Exception if an invalid handle is

  • 0

As of the MSDN spec, CloseHandle throws an Exception if an invalid handle is passed to it when it runs under a debugger.

Since I want to have clean code, I’ve inserted some code to catch it. However, it doesn’t work, the exception gets uncaught.

#include <windows.h>
#include <tchar.h>
#include <exception>
/* omitted code */
CloseHandle(myHandle); // close the handle, the handle is now invalid
try {
    success = CloseHandle(myHandle);
} catch (std::exception& e) {
    _tprintf(TEXT("%s\n"), e.what());
} catch (...) {
    _tprintf(TEXT("UNKNOWN\n"));
}

I get the following two errors from the debugger:

First-chance exception: 0xC0000008: An invalid handle was specified.

Uncaught exception: 0xC0000008: An invalid handle was specified.

I think that the first-chance exception is normal, since it gets fired before the catch statement should get it. However, the uncaught exception makes me wondering what’s actually wrong here.

  • 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-05-12T16:51:04+00:00Added an answer on May 12, 2026 at 4:51 pm

    You have two options:

    Option 1:
    Use SEH, you need to write something like this:

    __try
    {
      // closeHandle
    }
    __except(EXCEPTION_EXECUTE_HANDLER)
    {
      // print
    }
    

    Option 2:
    Use the compiler switch /EHa, which will instruct the compiler to emmit code which will allow you to handle SEH exception via C++ style exception handling:

    try
    {
     // close handle
    }
    catch (...)
    {
      // print
    }
    

    Edit:
    Note that CloseHandle() only raises an exception if a debugger is attached to your process.
    From the documentation:

    If the application is running under a debugger, the function will
    throw an exception if it receives either a handle value that is not
    valid or a pseudo-handle value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to MSDN (Section 11.3.6 of the C# spec ): Within an instance constructor
MSDN has the functionality I'm looking for here . Since microsoft recognizes the usefulness
From MSDN If there are no waiting threads, the wait handle remains signaled until
MSDN states that: Background threads are identical to foreground threads with one exception: a
The MSDN docs for volatile in Visual C++ indicate that writes have release semantics
MSDN on migrating legacy multithreaded applications (from this page on exception handling in threads):
MSDN have some word docs on how to correctly partition and layer an application.
MSDN categorizes var under Types . variables that are declared at method scope can
MSDN says this about the StackTrace property of the Exception class: The StackTrace property
MSDN documentation doesn't seem to have good coverage on ASP.net 4.5 support of HTML5

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.