Exceptions and file checking.

Solved my own issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}
Last edited on
http://www.cplusplus.com/doc/tutorial/exceptions/

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.
study up on discrete mathematics.
Bump. (Changed topic).
Last edited on
Topic archived. No new replies allowed.