Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

The goto-less goto!

3.27/5 (11 votes)
29 Apr 2010CPOL 1  
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:

C++
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)