June 2025
Intermediate to advanced
1093 pages
33h 24m
English
Unlike in the first example, the processing now completely stops; for empty_file, you see no result. This is because the exception from open() is only caught in main()—and thus the for loop in process was also prematurely terminated.
If you want the same behavior—that is, continuing the processing with the next file—then you must not leave the for loop in passing. You need error handling within the loop.
void process(const vector<string>& args) { if(args.size() == 0) { // expect parameters throw std::invalid_argument{"Command line argument missing"}; } else { for(const string filename : args) { cout << filename << ": "; try { cout << countWords(filename) << "\n"; } catch(std::exception &exc) { cout << "Error: " ...
Read now
Unlock full access