为什么下面的代码有效,但是如果我添加“fin.exceptions(ifstream::badbit | ifstream::failbit);”为了使第二次 try/catch 工作,我收到错误?

问题描述 投票:0回答:1

我的代码:

int main() {
    string path = "D:\\myFile.txt";
    Point pointOfIllusion(1, 2, 3);
    Point pointOfDesilusion(3, 2, 1);
    ofstream fout;
    
    fout.exceptions(ofstream::badbit | ofstream::failbit); **// everything is ok with this string**
    try {
        cout << "Trying to read the file\n";
        fout.open(path, ofstream::app); 
        cout << "The file has been successfully created and opened\n";
    }
    catch(const ofstream::failure& ex){
        cout << ex.what() << endl;
        cout << ex.code() << endl;
        perror ("Could not open the file");
        abort();
    }
    fout.write((char*)&pointOfIllusion, sizeof(Point));
    fout.write((char*)&pointOfDesilusion, sizeof(Point));

    fout.close();
    
    ifstream fin;
    fin.exceptions(ifstream::badbit | ifstream::failbit); **// If I put this, it causes an error, described below, but without this string the following try/catch doesn't work**
    try {
        cout << "Trying to read the file\n";
        fin.open(path);
        cout << "The file has been successfully opened\n";
    }
    catch (const ifstream::failure& ex) {
        cout << ex.what() << endl;
        cout << ex.code() << endl;
        perror ("Could not open the file");
        abort();
    }
    
    Point tempPoint;
    size_t length = 0;
    while (fin.read((char*)&tempPoint, sizeof(Point))) { **// The debugger throws the exception when I add the string above in this place, however I do not modify it at all**
        length++;
    }

    fin.clear();
    fin.seekg(0, ios::beg);
    
    Point* Points = new Point[length];
    for (size_t i = 0; i < length; i++)
        {
            fin.read((char*)&Points[i], sizeof(Point));
            Points[i].Print();
        }
        delete[] Points;
        
    fin.close();
    getchar();
    return 0;
}

我在循环中遇到错误:

while (fin.read((char*)&tempPoint, sizeof(Point))) {
    length++;
}

错误如下:

An exception was raised at address 0x0000007FFECCC3CF19 in WorkWithFiles.exe: Microsoft C++: std::ios_base::failure exception at memory address 0x0000000059CBAFEF80.
An unhandled exception occurred at address 0x0000007FFECCCCC3CF19 in WorkWithFiles.exe: Microsoft C++ exception: std::ios_base::failure at memory address 0x00000059CBAFEF80.

没有此代码:

fin.exceptions(ifstream::badbit | ifstream::failbit);

不存在此类错误,程序可以运行,但是在使用第二个

try
/
catch
块(带有
fin
对象)时,它不会捕获异常。只有
fout
有效。

你能告诉我,我做错了什么吗?

我尝试使用

try
/
catch
对象练习
ifstream
/
ofstream
,同时使用正确和错误的地址打开文件。

c++ exception try-catch ifstream ofstream
1个回答
1
投票

没有

fin.exceptions(ifstream::badbit | ifstream::failbit);

因此,当这条线保留在原处时,就会发生这种情况。此行告诉您的计算机,如果

fin
上的任何操作失败,则抛出异常(这里两种状态之间的差异并不重要)。

while (fin.read((char*)&tempPoint, sizeof(Point))) {

此行告诉您的计算机继续调用

read()
,直到
fin
不再处于良好状态。然而,根据《计算机编程黄金法则》,“你的计算机总是完全按照你告诉它去做的事情而不是你想让它做的事情”,失败的
read()
最终会抛出异常。这就是为什么这一行最终抛出异常的原因:因为这就是您告诉计算机在
fin
的状态更改为失败状态时执行的操作。事实上,有一个循环调用
read()
,直到发生这种情况,这一点并没有改变。异常由
fin
本身抛出,作为
read()
的一部分,它永远不会返回并且抛出异常,因此
while
循环永远不会进行更改来检查
fin
的状态。

© www.soinside.com 2019 - 2024. All rights reserved.