try,catch,throw语句应该怎么写??

gonghai78 2004-08-10 04:46:39
pTable=new struct TableStruct;
nTableCont=GetFile((char*)LPCTSTR(csFileName),pTable);

if(!nTableCont)
{
MessageBox("文件不能打开!","错误信息");
FreeMem();
PostMessage(WM_CLOSE);
}
else
{
nTableIndex=0;
while(pTable->pUpTable!=NULL)
pTable=pTable->pUpTable;

nIndexUser=0;
UpdateRecord();
}
}
else语句中出现内存泄露,怎么解决?
...全文
598 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
落入凡间的猪 2004-08-10
  • 打赏
  • 举报
回复
try
{

}
catch(...)
{

}
nineclock 2004-08-10
  • 打赏
  • 举报
回复
CFile* pFile = NULL;

// Constructing a CFile object with this override may throw
// a CFile exception, and won't throw any other exceptions.
// Calling CString::Format() may throw a CMemoryException,
// so we have a catch block for such exceptions, too. Any
// other exception types this function throws will be
// routed to the calling function.

TRY
{
pFile = new CFile(_T("C:\\WINDOWS\\SYSTEM.INI"),
CFile::modeRead | CFile::shareDenyNone);

DWORD dwLength = pFile->GetLength();

CString str;
str.Format(_T("Your SYSTEM.INI file is %u bytes long."),
dwLength);

AfxMessageBox(str);
}
CATCH(CFileException, pEx)
{
// Simply show an error message to the user.

pEx->ReportError();
}
AND_CATCH(CMemoryException, pEx)
{
// We can't recover from this memory exception, so we'll
// just terminate the app without any cleanup. Normally, an
// an application should do everything it possibly can to
// clean up properly and _not_ call AfxAbort().

AfxAbort();
}
END_CATCH

// If an exception occurs in the CFile constructor,
// the language will free the memory allocated by new
// and will not complete the assignment to pFile.
// Thus, our clean-up code needs to test for NULL.

if (pFile != NULL)
{
pFile->Close();
delete pFile;
}

lixiaosan 2004-08-10
  • 打赏
  • 举报
回复
http://www.vchelp.net/wyy/tour/teach_sp_34.htm
libraworm 2004-08-10
  • 打赏
  • 举报
回复
msdn索引里面查找try
wfq771105 2004-08-10
  • 打赏
  • 举报
回复
up
快乐鹦鹉 2004-08-10
  • 打赏
  • 举报
回复
try
{
if(!nTableCont)
{
MessageBox("文件不能打开!","错误信息");
FreeMem();
PostMessage(WM_CLOSE);
}
else
{
nTableIndex=0;
while(pTable->pUpTable!=NULL)
pTable=pTable->pUpTable;

nIndexUser=0;
UpdateRecord();
}
}
catch(_com_error &e)
{
}
DentistryDoctor 2004-08-10
  • 打赏
  • 举报
回复
要防止因为异常产生的内存泄漏,可以使用智能指针,也可以用
__try
{
}
__finally
{
}
《Windows核心编程》一书第23~25章是很好的参考资料。
Kudeet 2004-08-10
  • 打赏
  • 举报
回复
TRY
{
......
}
CATCH(CDBException, e)
{
.......
}END_CATCH
或在
catch(_com_error &e)
{
...
}

16,548

社区成员

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

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

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