J
James Kanze
We can (and probably would) do the same in C++. A missing file isn't
necessarily an exceptional situation. Now if it were an exceptional
situation and the open was part of a group of resource allocations, C
code would have to include had written clean up code and quite likely a
"goto error". In C++, we'd just throw and rely on the automatic clean
up RAII provides.
The problem isn't the missing file. The problem is when you
find something in it which makes further processing impossible,
and your 10 or more levels deep in "use". At that point, in C,
you have to propagate the error up manually, to ensure that the
close is called (and that any memory you allocated in the
intermediate functions is freed). In C++, the problem more or
less takes care of itself (providing you're using the
established idioms).
This is one of the things that make C so much more difficult
than C++.