try{ // CHAPTER 16 EXCEPTIONS... TRY, THROW, AND CATCH.
NameFile.open("Halo The Master Chief Collection.txt", ios::in);
if (!NameFile) {
throw string("ERROR: Cannot open description file.\n""Therefore, the description of this game is missing.\n");
}
getline(NameFile, Input);
while (NameFile) {
cout << Input << endl;
getline(NameFile, Input);
}
NameFile.close(); // CLOSE THE FILE.
}catch(string exceptionString)
{
cout << exceptionString;
}
Incidentally, if the user enters an invalid number like in your example i would not throw an exception. This is not an exceptional circumstance really. Trying to open a file that isn't there might be a good place to use exceptions.