VC Serialize格式出错 程序关闭处理方法

VC Serialize时,因为格式问题(大量文件时,难免有格式出错),导致程序直接关闭。

CArchive ar( &f, CArchive::load);
Data.Serialize(ar);

结果格式不对,程序直接退出

我想如果格式不对,能否就跳过,而不是直接导致程序关闭。

试试这个吧
https://www.docin.com/p-238065713.html

使用try捕获该捕获的异常

捕获如下

try {

            CArchive ar(&f, CArchive::load);
            Data.Serialize(ar);
    }
    catch (CArchiveException* e)
    {
        e->ReportError();
        e->Delete();
        if (e->m_cause == CArchiveException::endOfFile)
        {
            cout << "已读到文件尾";
        }
        else if (e->m_cause == CArchiveException::badIndex)
        {
            cout << "文件格式错";
        }
        else if (e->m_cause == CArchiveException::readOnly)
        {
            cout << "文件只能是读";
        }
        else if (e->m_cause == CArchiveException::writeOnly)
        {
            cout << "文件只能是写" ;
        }
        else if (e->m_cause == CArchiveException::badClass)
        {
            cout << "文件只能是读";
        }
        else if (e->m_cause == CArchiveException::readOnly)
        {
            cout << "Tried to read an object into an object of the wrong type";
        }
        else if (e->m_cause == CArchiveException::badSchema)
        {
            cout << "Tried to read an object with a different version of the class";
        }
        else
            cout << "CArchiveException::generic";
    }

各位大佬,我用release模式运行后,发现try catch(...)是可以捕获的,仅仅是debug模式无法捕获。所以这个事情暂时就解决了。感谢各位。