65.9K
CodeProject is changing. Read more.
Home

The goto-less goto!

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.27/5 (11 votes)

Apr 29, 2010

CPOL
viewsIcon

10950

Use C++ exceptions:try{ if (condition1_fails) throw 1; ... if (condition2_fails) throw 2; ... ... if (conditionN_fails) throw N; PerformActionOnAllSuccess(); DoNormalCleanup();}catch (int condition){ printf("The condition %d fails!\n",...

Use C++ exceptions:
try
{
   if (condition1_fails) throw 1;
   ...
   if (condition2_fails) throw 2;
   ...
   ...
   if (conditionN_fails) throw N;

   PerformActionOnAllSuccess();
   DoNormalCleanup();
}
catch (int condition)
{
   printf("The condition %d fails!\n", condition);
   DoFailedCleanup();
}
This way you have the overhead of the C++ exeption handling mechanism, but you'll have more control on what to do: you can for instance throw different exception types depending on the failed condition and catch them on different catch blocks.