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
其他的就可以正常处理异常

解决方案 »

  1.   

    lz查一下CStdioFile,看看它抛出的是不是CFileException,可能是别的异常类
      

  2.   

    我用try
    {
    }catch(...)
    {
    }都不行,
    是不是不支持异常呢
      

  3.   

    我看了一下,应该是可以的。
    出了什么错?s_name的值是?
      

  4.   

    TRY
    {
        // 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
      

  5.   

    因为哪个文件就不存在,在执行
    cf.ReadString(row);
    就会报错,说assert(str!=NULL)
    可就是不去catch{}
      

  6.   

    TRY
    {
        // 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
    我也试了,也不行,是不是需要设置什么参数吗
      

  7.   

    提示这种错误 Debug Assertion Fail!
      

  8.   

    注意Open函数的第三个参数
    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.这下理解了吧....
      

  9.   

    if(!cf.Open(s_name,CStdioFile::modeRead ))
    {
    AfxMessageBox("asfasdf");
        return;
    }
    文件都不存在,你还往下读什么啊,加一句return这个错误不是运行时的异常,是个内存指针错误,你看到的提示框是debug下的,release下就不是这样了
      

  10.   

    virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL );pError传什么参数才可以让它throw e呢
      

  11.   

    CFileException ex;   // open the source file for reading   if (!sourceFile.Open("11",
          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;
       }
      

  12.   

    CFile::Open()在错误情况下返回FALSE,不是抛出异常   
    CFile::CFile()在错误情况下才抛出异常。   
      

  13.   

    也就是你改程这样也行: CString row;
    try
    {
    CFile cf("11",CStdioFile::modeRead);

    AfxMessageBox("asfasdf");

    }catch(CFileException  &e)
    {
    TCHAR szCause[255];
    e.GetErrorMessage(szCause, 255);
    MessageBox(szCause); }