Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

The goto-less goto!

Rate me:
Please Sign up or sign in to vote.
3.27/5 (11 votes)
29 Apr 2010CPOL 10K   2   6
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)


Written By
Engineer Cutlite Penta S.r.l.
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 Not to repeat the other reasons for ... Pin
Wolfgang_Baron9-Feb-11 5:35
professionalWolfgang_Baron9-Feb-11 5:35 
GeneralI am in complete agreement with those who say exceptions sho... Pin
jim lahey26-Jan-11 3:45
jim lahey26-Jan-11 3:45 
General"It is not wise to throw exception" - Really? It's strange b... Pin
Sauro Viti24-Jan-11 22:31
professionalSauro Viti24-Jan-11 22:31 
GeneralReason for my vote of 2 As Klaus said, it is not wise to thr... Pin
Pravin Patil, Mumbai24-Jan-11 21:38
Pravin Patil, Mumbai24-Jan-11 21:38 
GeneralReason for my vote of 1 First throwing exceptions is very ex... Pin
Klaus Luedenscheidt24-Jan-11 18:48
Klaus Luedenscheidt24-Jan-11 18:48 
GeneralRe: Have you tried it? One can throw a lot of exceptions in a si... Pin
Eddy Vluggen12-Oct-11 7:12
professionalEddy Vluggen12-Oct-11 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.