Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

The goto-less goto!

, 29 Apr 2010
Rate this:
Please Sign up or sign in to vote.
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.

License

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

About the Author

Sauro Viti
Engineer
Italy Italy
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 29 Apr 2010
Article Copyright 2010 by Sauro Viti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid