• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Breaking out of nested functions

## Recommended Posts

This is probably not a good way to do this, but... I''ve got functions that call functions, and recursion and etc. And when I end up who knows how many functions deep, I''m going to have an error condition. What I want is to immediately jump back to a "top level" position. I could pass the error condition to the calling function, which passes the error condition back to its calling function, which passes... But that sounds like a pain. What I want sounds for all the world like a goto, but I don''t think a goto would quite work, unless I did some other wierd things, like manually deallocate the stack. So far what I''ve thought up sounds really nasty. Anybody know a way to do this?

##### Share on other sites
In c++ you can throw an exception and catch it in the outter most function call.
But alot of overhead goes into building and destorying function entries and exits. Which the C++ compiler has to predict but even more over head when it unravels the function stack.

##### Share on other sites
That sounds promising. As far as overhead goes, if it''s something that degrades performance when I get the error condition, I think I can live with that. But if it''s something that degrades performance when everything''s running fine, then that deserves a closer look.

##### Share on other sites
Exceptions, my friend.

This is exactly the situation that they were designed for.

      class some_exception{ // specifics};void inner3(){  if(/* some error check here*/)    throw some_exception();  else  {    // normal function logic  }}void inner2(){  inner3();}void inner1(){  inner2();}void outer(){  try  {    inner1();  }  catch(some_exception& e)  {    // handle error here  }}

[edited by - daerid on October 23, 2002 1:36:09 PM]

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627665
• Total Posts
2978530

• 10
• 10
• 12
• 22
• 13