Read a file in try catch block : file IO Exception « File Stream « C++ Tutorial






#include <fstream>
#include <iostream>
using namespace std;

int main () 
{
  try{
      char buffer[256];
      ifstream myfile ("test.txt");

      while (! myfile.eof() )
      {
        myfile.getline (buffer,100);
        cout << buffer << endl;
      }
  }catch(...){
     cout << "There was an error !\n";
  }
  return 0;
}
This outputting a line.








12.22.file IO Exception
12.22.1.Read a file in try catch block
12.22.2.Handle basic exception
12.22.3.throw exception in function cascading
12.22.4.checks for errors opening file
12.22.5.handles errors during input and output