我正在尝试处理数据库连接错误。如果CDatabase::Open(或CDatabase::OpenEx)失败,则通常会抛出CDBException。但是有东西。我无法应付!

这是我的代码:

try
{
    CDatabase db;
    db.OpenEx(L"DSN=INVALID_DSN;UID=noname", CDatabase::noOdbcDialog);
    wprintf(L"Connection established.\n"
            L"DSN used: %s\n"
            L"DB name:  %s\n",
            db.GetConnect().GetBuffer(),
            db.GetDatabaseName().GetBuffer());
    db.Close();
    puts("Connection closed.");
}
catch (CDBException& e)
{
    e.GetErrorMessage(buf, BUF_SIZE);
    wprintf(L"CDBException: %s\n", buf);
}
catch (CException& e)
{
    e.GetErrorMessage(buf, BUF_SIZE);
    wprintf(L"CException: %s\n", buf);
}
catch (std::exception& e)
{
    printf("STD exception: %s\n", e.what());
}
// this section is the only way to handle exception thrown by the CDatabase::OpenEx
//catch (...)
//{
//    puts("Unknown exception");
//}

如果最后一个catch被评论,我会收到错误通知:

c++ - 具有适当的 “catch”时未处理的CDBException-LMLPHP

我使用Visual Studio 2013(版本12.0.40629.00更新5)

最佳答案

您将需要使用MFC特定的异常处理宏:TRYCATCHEND_CATCH。 MFC异常是仅指针的,并且只能由特殊宏删除。

参见example here

编辑:内联示例:

TRY
{
   // Do something to throw an exception.
   AfxThrowUserException();
}
CATCH(CException, e)
{
   if (m_bPassExceptionsUp)
      THROW_LAST();

   if (m_bReturnFromThisFunction)
      return;

   // Not necessary to delete the exception e.
}
END_CATCH

关于c++ - 具有适当的 “catch”时未处理的CDBException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37250503/

10-15 12:48