C++ 在C+;中使用两个文件流处理异常+;

C++ 在C+;中使用两个文件流处理异常+;,c++,exception,try-catch,fstream,C++,Exception,Try Catch,Fstream,我正在使用Visual Studio 2012构建一个处理输入文件和输出文件的程序 我是这样实施的: ifstream inputFile; ofstream outputFile; inputFile.exceptions ( ifstream::failbit | ifstream::badbit ); try { // some codes here inputFile.open(inputFileName.c_str()); cout << "Inp

我正在使用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;
}   

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

您的问题是
ifstream::failure
ofstream::failure
是相同的类型(从ios_库继承给这两个源)

由于是同一个异常,编译器会抱怨


顺便说一句,您应该避免不必要的副本。

您的问题是
ifstream::failure
ofstream::failure
是相同的类型(从ios_库继承给它们)

由于是同一个异常,编译器会抱怨


顺便说一句,您应该避免不必要的副本。

您的问题是
ifstream::failure
ofstream::failure
是相同的类型(从ios_库继承给它们)

由于是同一个异常,编译器会抱怨


顺便说一句,您应该避免不必要的副本。

您的问题是
ifstream::failure
ofstream::failure
是相同的类型(从ios_库继承给它们)

由于是同一个异常,编译器会抱怨


顺便说一句,您应该避免不必要的副本。

正如您所看到的,抛出的异常类型是相同的。但是,由于您的检查是在文件打开时进行的,因此您可以毫无例外地进行检查,不是吗?比如:

#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串输入文件名{“/notexists”};
字符串outputFileName{“/notexistsandprobblynotwriteable”};
ifstream inputFile{inputFileName};
如果(!inputFile){

cerr正如您所看到的,抛出的异常类型是相同的。但是,由于您的检查是在接近文件打开时进行的,因此您可以在没有异常的情况下进行检查,例如:

#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串输入文件名{“/notexists”};
字符串outputFileName{“/notexistsandprobblynotwriteable”};
ifstream inputFile{inputFileName};
如果(!inputFile){

cerr正如您所看到的,抛出的异常类型是相同的。但是,由于您的检查是在接近文件打开时进行的,因此您可以在没有异常的情况下进行检查,例如:

#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串输入文件名{“/notexists”};
字符串outputFileName{“/notexistsandprobblynotwriteable”};
ifstream inputFile{inputFileName};
如果(!inputFile){

cerr正如您所看到的,抛出的异常类型是相同的。但是,由于您的检查是在接近文件打开时进行的,因此您可以在没有异常的情况下进行检查,例如:

#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串输入文件名{“/notexists”};
字符串outputFileName{“/notexistsandprobblynotwriteable”};
ifstream inputFile{inputFileName};
如果(!inputFile){

cerr这不是一个好主意。您不应该将
failbit
设置为抛出,因为
failbit
将在文件末尾设置(并且您不希望文件末尾出现异常)。通常,当您无法打开文件时,您不希望出现异常;您希望立即处理错误。我发现.Thx用于建议James。这不是一个好主意。您不应该将
failbit
设置为抛出,因为
failbit
将在文件末尾设置(并且您不希望文件末尾出现异常)。通常,当您无法打开文件时,您不希望出现异常;您希望立即处理错误。我发现.Thx用于建议James。这不是一个好主意。您不应该将
failbit
设置为抛出,因为
failbit
将在文件末尾设置(并且您不希望文件末尾出现异常)。通常,当您无法打开文件时,您不希望出现异常;您希望立即处理错误。我发现.Thx用于建议James。这不是一个好主意。您不应该将
failbit
设置为抛出,因为
failbit
将在文件末尾设置(并且您不希望文件末尾出现异常)。通常情况下,当无法打开文件时,您不希望出现异常;您希望立即处理错误。我发现.Thx用于建议James。比副本本身更重要的是:您应该捕获常量引用以避免切片。比副本本身更重要的是:您应该捕获常量引用以避免切片比副本本身更重要的是:你应该捕获常量引用以避免切片。比副本本身更重要的是:你应该捕获常量引用以避免切片。