分享

VC一个可以catch数据库错误消息和其他错误消息的宏(转)

 汇英四方 2013-12-02

参数b是显示catch到的错误后的后续处理代码

参数c是数据库连接指针_ConnectionPtr的对象,用于捕捉数据库错误消息

其中用到的下面两个函数,见本专栏其他文章。

  1. GetProviderError(_ConnectionPtr)  
  1. <pre name="code" class="cpp">GetComError(_com_error &e)</pre>  
  2. <pre></pre>  
  3. <p></p>  
  4. <pre></pre>  
  5. <p></p>  
  6. <p>#define MYCATCH(b,c) \</p>  
  7. <p></p>  
  8. <pre name="code" class="cpp">   catch(const _com_error& e)\  
  9. {\  
  10.     CString sBuff = GetProviderError(c);\  
  11.     if(sBuff.IsEmpty())\  
  12. {\  
  13.     sBuff=GetComError(e);\  
  14. }\  
  15.     AfxMessageBox(sBuff);\  
  16.     b\  
  17. }\  
  18.     catch(COleException* e)\  
  19. {\  
  20.     if(e)\  
  21. {\  
  22.     CString szMsg;\  
  23.     e->GetErrorMessage(szMsg.GetBuffer(255), 255);\  
  24.     szMsg.ReleaseBuffer();\  
  25.     AfxMessageBox(szMsg);\  
  26.     e->Delete();\  
  27. }\  
  28.     b\  
  29. }\  
  30.     catch(COleDispatchException* e)\  
  31. {\  
  32.     if(e)\  
  33. {\  
  34.     AfxMessageBox(e->m_strDescription);\  
  35.     e->Delete();\  
  36. }\  
  37.     b\  
  38. }\  
  39.     catch(CMemoryException* e)\  
  40. {\  
  41.     if(e) e->Delete();\  
  42.     AfxMessageBox(_T("Out-of-memory."));\  
  43.     b\  
  44. }\  
  45.     catch(LPCTSTR e)\  
  46. {\  
  47.     AfxMessageBox(e);\  
  48.     b\  
  49. }\  
  50.     catch(...)\  
  51. {\  
  52.     AfxMessageBox(TEXT("<Unexpected Error>"));\  
  53.     b\  
  54. }</pre><br>  
  55. <br>  
  56. <p></p>  
更多 0
    • 本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多