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.

Edit
Report