I found the following code in a C program:
while (1)
{
do_something();
if (was_an_error()) break;
do_something_else();
if (was_an_error()) break;
[...]
break;
}
[cleanup code]
Here while(1)
is used as local emulation of "finally". You can also write this using goto
s:
do_something()
if (was_an_error()) goto out;
do_something_else()
if (was_an_error()) goto out;
[...]
out:
[cleanup code]
I thought the goto solution is a usual idiom. I have seen several occurrences of this idiom in the kernel sources and it is also mentioned in Diomidis Spinellis' "Code Reading" book.
My question is: What solution is better? Is there any specific reason to use the while(1)
solution?
Question 943826 doesn't answer my question.