【问题标题】:Handling exception(s) with two file stream in C++在 C++ 中使用两个文件流处理异常
【发布时间】:2014-04-11 12:17:16
【问题描述】:

我正在使用 Visual Studio 2012 构建一个处理输入文件和输出文件的程序。

我是这样实现的:

ifstream inputFile;
ofstream outputFile;

inputFile.exceptions ( ifstream::failbit | ifstream::badbit );
try
{
    // some codes here
    inputFile.open(inputFileName.c_str());
    cout << "Input file opened" << endl;

    outputFile.open(outputFileName.c_str());
    cout << "Output file opened" << endl;
}
catch (ifstream::failure e)
{
    cerr << "Failed to open input file" << endl;
    return -1;
}
catch (ofstream::failure e)
{
    cerr << "Failed to open output file" << endl;
    return -1;
}   

并且出现编译错误:

error C2312: 'std::ios_base::failure' : is caught by 'std::ios_base::failure' at line 248

如何使用两个异常源实现 try-catch?

【问题讨论】:

  • 不是一个好主意。您永远不应该将failbit 设置为抛出,因为failbit 将设置在文件末尾(并且您不希望文件末尾出现异常)。当您无法打开文件时,您通常不希望出现异常;您想立即处理错误。
  • 我发现了。谢谢詹姆斯的建议。

标签: c++ exception try-catch fstream


【解决方案1】:

你的问题是ifstream::failureofstream::failure是同一个类型(从ios_base继承给他们两个),

由于是同一个异常,编译器报错。

顺便说一句,您应该catch by const reference 以避免不必要的副本。

【讨论】:

  • 比副本本身更重要:您应该通过 const 引用来捕获以避免切片。
【解决方案2】:

如您所见,抛出的异常类型是相同的。但是,由于您的检查是在文件打开附近完成的,因此您可以毫无例外地进行检查,不是吗?喜欢:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
  string inputFileName { "/notexists" };
  string outputFileName { "/notexistsandprobablynotwritable" };
  ifstream inputFile { inputFileName };
  if( !inputFile ) {
    cerr << "Failed to open input file" << endl;
    return -1;
  }
  cout << "Input file opened" << endl;
  ofstream outputFile { outputFileName };
  if( !outputFile ) {
    cerr << "Failed to open output file" << endl;
    return -1;
  }
  cout << "Output file opened" << endl;
}

或者,如果你真的需要异常,你可以自己抛出不同的异常,在开放的站点:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

template<typename Stream, typename Exception = typename Stream::failure>
  void try_open(Stream& s, string filename) {
      s.open(filename);
      if( !s )
        throw Exception( string("cannot open ")+filename );
  }

struct input_exception: ifstream::failure { input_exception(const string& s): ifstream::failure(s) {} };
struct output_exception: ofstream::failure { output_exception(const string& s): ofstream::failure(s) {} };

int main() {
  string inputFileName { "/notexists" };
  string outputFileName { "/notexistsandprobablynotwritable" };
  try {
    ifstream inputFile;
    try_open<ifstream, input_exception>(inputFile, inputFileName);
    cout << "Input file opened" << endl;
    ofstream outputFile;
    try_open<ofstream, output_exception>(outputFile, outputFileName);
    cout << "Output file opened" << endl;
  } catch(const output_exception& e) {
      cerr << "output exception!\n" << e.what() << "\n";
  } catch(const input_exception& e) {
      cerr << "input exception!\n" << e.what() << "\n";
  } catch(const exception& e) {
      cerr << "exception!\n" << e.what() << "\n";
  }
}

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 2016-09-27
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多