I'm reading the recently released "Advanced Mac OS X Programming: The Big Nerd Ranch Guide" by Mark Dalrymple and have reached a section about using goto as an error handling technique. To quote the book:
He then provides the following pseudocode (may contain typing errors because I can't compile it)
I'm wondering how others feel about this approach.
Unfortunately, too many in the programming industry have been trained to have a knee-jerk reaction to goto, when in many cases it can lead to much more readable cleanup than the alternatives.
He then provides the following pseudocode (may contain typing errors because I can't compile it)
Code:
int someFunction( void )
{
result = failure;
blah = allocate_some_memory();
if( do_something( blah ) == failure )
{
goto bailout;
}
ack = open_a_file();
if( process_file( blah, ack ) == failure )
{
goto bailout;
}
hoover = do_something_else();
if( have_fun( blah, hoover ) == failure )
{
goto bailout;
}
// we survived!
result = success;
bailout:
if( blah ) free_the_memory( blah );
if( ack ) close_the_file( ack );
if( hoover ) clean_this_up( hoover );
return result;
}
I'm wondering how others feel about this approach.