捕捉异常问题

lengx7 2011-01-17 09:42:47
程序现在添加了以下语句连接mysql
#include <winsock.h>
#include "mysql.h"
#pragma comment(lib, "libmysql.lib")

try
{
MySqlConn();//连接,操作mysql
}
catch(???)
{
EditAdd(???);
}

请问用什么样的办法才可以捕获所有的异常,并把异常的错误信息在Edit里显示出来呢?
...全文
172 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
BombZhang 2011-01-17
  • 打赏
  • 举报
回复

#define MYCATCH(b,c) \
catch(const _com_error& e)\
{\
CString sBuff = GetProviderError(c);\
if(sBuff.IsEmpty())\
{\
sBuff=GetComError(e);\
}\
AfxMessageBox(sBuff);\
b\
}\
catch(COleException* e)\
{\
if(e)\
{\
CString szMsg;\
e->GetErrorMessage(szMsg.GetBuffer(255), 255);\
szMsg.ReleaseBuffer();\
AfxMessageBox(szMsg);\
e->Delete();\
}\
b\
}\
catch(COleDispatchException* e)\
{\
if(e)\
{\
AfxMessageBox(e->m_strDescription);\
e->Delete();\
}\
b\
}\
catch(CMemoryException* e)\
{\
if(e) e->Delete();\
AfxMessageBox(_T("Out-of-memory."));\
b\
}\
catch(LPCTSTR e)\
{\
AfxMessageBox(e);\
b\
}\
catch(...)\
{\
AfxMessageBox(TEXT("<Unexpected Error>"));\
b\
}

CString GetProviderError(_ConnectionPtr pConnection)
{
CString sErrors;
if(pConnection != NULL)
{
ADODB::ErrorsPtr ptrErrors = pConnection->Errors;
long lCount = ptrErrors->Count;
ADODB::ErrorPtr ptrError = NULL;
CString sError;
for(long n = 0; n < lCount; n++)
{
ptrError = ptrErrors->GetItem(n);
sError.Format(
_T("%s\nState: %s, Native: %d, Source: %s"),
(LPCTSTR)ptrError->Description,
(LPCTSTR)ptrError->SQLState,
ptrError->NativeError,
(LPCTSTR)ptrError->Source
);
sErrors += sError + TEXT("\n\n");
}
}

if(!sErrors.IsEmpty())
sErrors = sErrors.Left(sErrors.GetLength()-2);

return sErrors;
}



这个宏的参数b是对错误如何处理,c是数据库connection指针
比如:
try
{
...
}
MYCATCH(return FALSE;,m_pConnection)
renxu350 2011-01-17
  • 打赏
  • 举报
回复
1#楼正解
wonder2008 2011-01-17
  • 打赏
  • 举报
回复
catch(...)
BombZhang 2011-01-17
  • 打赏
  • 举报
回复
每次只会catch到一个错误,实在catch不到了才会轮到处理catch(...)
lengx7 2011-01-17
  • 打赏
  • 举报
回复
catch(COleException* e)\
{\
if(e)\
{\
CString szMsg;\
e->GetErrorMessage(szMsg.GetBuffer(255), 255);\
szMsg.ReleaseBuffer();\
AfxMessageBox(szMsg);\
e->Delete();\
}\


catch(...)\
{\
AfxMessageBox(TEXT("<Unexpected Error>"));\
b\
}

catch(...)这样可以捕捉到所有异常的话,本次的错误信息是什么呢?
如上文所写的catch(COleException* e);e->GetErrorMessage=错误信息的话,catch(...)格式的错误信息放在哪里呢?
BombZhang 2011-01-17
  • 打赏
  • 举报
回复
我上面给你的代码里不是有好多AfxMessageBox吗?这个是弹出错误消息,你可以用EditAdd代替,把错误消息显示到Edit控件
lengx7 2011-01-17
  • 打赏
  • 举报
回复
AfxMessageBox(sss);中的sss是那里的呢?
BombZhang 2011-01-17
  • 打赏
  • 举报
回复
每个catch里都有AfxMessageBox(sss);
把这个消息放到EditAdd(sss)
lengx7 2011-01-17
  • 打赏
  • 举报
回复
catch(...)
捕获异常后怎么把错误信息输出呢?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧