无法在 ADO OLEDB 中捕获 CommitTrans 当它给出0xc0000005错误时

can't trap CommitTrans in ADO OLEDB when it gives 0xc0000005 error

本文关键字:0xc0000005 错误 CommitTrans ADO OLEDB      更新时间:2023-10-16

当我尝试执行CommitTrans时,当异常代码0xc0000005发生时,'catch'不会捕获:

_ConnectionPtr connection = NULL;
CoInitialize(NULL);
connection.CreateInstance(__uuidof(Connection));
connection->CursorLocation = adUseClient;
connection->Open(sConnectionString,L"",L"",0);
connection->Execute(sSQL,NULL,adCmdText);
try
{
connection->CommitTrans();  // <- 0xc0000005 1. Why? 2. 'catch' doens't trap
}
catch(...)
{
DWORD dwErr;
dwErr;
}
recordset->Close();
recordset = NULL;
connection->Close();
connection=NULL;
CoUninitialize();
    为什么CommitTrans在这里失败了?
  1. 为什么"catch"不捕获失败?

要捕获0xc0000005异常,您需要使用结构化异常处理。

详情见此处

Try the following code and you will see that the program print "__except" rather than "catch".
#include <windows.h>
#include <iostream>
void foo()
{
    try
    {
        int *p = NULL;
        *p = 1;
    }
    catch ( ... )
    {
        std::cout << "catchn";
    }
}
void bar()
{
    __try
    {
        foo();
    }
    __except ( EXCEPTION_EXECUTE_HANDLER )
    {
        std::cout << "__exceptn";
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    bar();
    std::cout << "successn";
    return 0;
}