I'm writing a library function that does some file i/o (using the Windows API).
Granted, such a function will have to open files, create files, read files, and allocate memory for buffers, all of which have the distinct possibility of failing for one reason or another. I want to write proper code that anticipates and can deal with any problems, but I'm not sure which way to do this.
The really simple, cumbersome way I started was something like (pseudocode)
But somehow having up to a dozen nested if statements seemed wrong...
bool FileFunc ( string infilename, string outfilename, ... )
bool result = false;
<open input file for reading>
if ( input file is open )
<open output file for writing>
if ( output file is open )
if ( buffer is allocated )
<other code, etc.>
if ( function succeeds )
result = true;
<free buffer memory>
<close output file>
<close input file>
I also considered
But then how would I close files and free memory unless I included the same cleanup function calls in every "if" block?
int FileFunc ( ... )
if ( input file is NOT open)
if ( buffer is NOT allocated )
(or setting a global error variable to such values and just returning true/false, like errno...)
(In addition, I know the WinAPI supports exception raising / handling...)
So my question is, how would you recommend I handle numerous possible error conditions in a single function?