Line 7 can potentially fail. The open method does not return any value. However, we can use the is_open method to check to see if the file associated with the ifstream is open. If the file fails to open what should we do?
There are only two options. We absolutely cannot move forward to read the numbers (since there is no file). We can choose to return 0 to indicate no number is read. However, this return code is ambiguous. It is possible that the file exists, but it contains no numbers. The second option is to “throw an exception” to let the caller know that the file fails to open.
Line 11 is another potential place for an error. What if there is no integer to read, or there is something that is non-integer in the file? Unlike scanf, the >> operator does not indicate whether a read operation was successful or not.
The means to detect a problem is not important here. The important question is what to do when there is a non-integer in the file. Should we skip to the next integer, or give up right away?
Line 13 the last place that can potentially have a problem. If we have read n integers, but the file is not at its end, what should we do? Is this an exception condition? If we just return n, it is ambiguous because the file may really only have n integers.