CStdioFile cf;
CString row;
try
{
if(!cf.Open(s_name,CStdioFile::modeRead ))
{
AfxMessageBox("asfasdf");
}
cf.ReadString(row);
}catch(CFileException &e)
{
TCHAR szCause[255];
e.GetErrorMessage(szCause, 255);
MessageBox(szCause); }每次出错就直接显示,跳不到CATCH里面.真奇怪,哥们帮帮忙.我用VC2005
其他的就可以正常处理异常
CString row;
try
{
if(!cf.Open(s_name,CStdioFile::modeRead ))
{
AfxMessageBox("asfasdf");
}
cf.ReadString(row);
}catch(CFileException &e)
{
TCHAR szCause[255];
e.GetErrorMessage(szCause, 255);
MessageBox(szCause); }每次出错就直接显示,跳不到CATCH里面.真奇怪,哥们帮帮忙.我用VC2005
其他的就可以正常处理异常
{
}catch(...)
{
}都不行,
是不是不支持异常呢
出了什么错?s_name的值是?
{
// Do something to throw an exception.
}
CATCH(CException, e)
{
if (m_bPassExceptionsUp)
THROW_LAST(); if (m_bReturnFromThisFunction)
return; // Not necessary to delete the exception e.
}
END_CATCH
cf.ReadString(row);
就会报错,说assert(str!=NULL)
可就是不去catch{}
{
// Do something to throw an exception.
}
CATCH(CException, e)
{
if (m_bPassExceptionsUp)
THROW_LAST(); if (m_bReturnFromThisFunction)
return; // Not necessary to delete the exception e.
}
END_CATCH
我也试了,也不行,是不是需要设置什么参数吗
CFile::Open
virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL );While the CFile constructor will throw an exception in an error condition, Open will return FALSE for error conditions. Open can still initialize a CFileException object to describe the error, however. If you don’t supply the pError parameter, or if you pass NULL for pError, Open will return FALSE and not throw a CFileException. If you pass a pointer to an existing CFileException, and Open encounters an error, the function will fill it with information describing that error. In neither case will Open throw an exception.这下理解了吧....
{
AfxMessageBox("asfasdf");
return;
}
文件都不存在,你还往下读什么啊,加一句return这个错误不是运行时的异常,是个内存指针错误,你看到的提示框是debug下的,release下就不是这样了
CFile::modeRead | CFile::shareDenyWrite, &ex))
{
// complain if an error happened
// no need to delete the ex object TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
cout << "Couldn't open source file: ";
cout << szError;
return 1;
}
CFile::CFile()在错误情况下才抛出异常。
try
{
CFile cf("11",CStdioFile::modeRead);
AfxMessageBox("asfasdf");
}catch(CFileException &e)
{
TCHAR szCause[255];
e.GetErrorMessage(szCause, 255);
MessageBox(szCause); }