Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I make this "define" call my function even if exception thrown

Status
Not open for further replies.

6tr6tr

Programmer
Jan 23, 2003
16
US
I've defined an alias for some code to make it easier for the users/developers to do certain actions.

So here's what i have now:

#define manage( val ) bool managed = false; while ( MyClass::manage( val, managed ) )

And then I can use the syntax:

manage( someObj )
{
...some code in here
}

And it will call "manage" both at the beginning bracket and the end (since it's a loop). Here's the problem: if an exception is thrown inside the codeblock (see above "...some code here" ), then it doesn't call "manage" at the end of the code block.

I can do the following, but I MUCH prefer the cleaner, more regular syntax if possible.

#define manage( val ) bool managed = false; try { while ( MyClass::manage( val, managed ) )
#define end_manage( val ) } catch (... ) { MyClass::manage, managed ); }

which allows me the syntax:

manage( someObj )
{
...some code in here
} end_manage( someObj );

and that's not bad, but I MUCH prefer it with just "manage" and the brackets. So my question:

Can i do something equivalent to:

#define manage( val ) bool managed = false; try { while ( MyClass::manage( val, managed ) ) $code_block } catch (...) { MyClass::manage, managed ); }

where "$code_block" signifies to the compiler that's the code in between the brackets?
 
I'm too tired to test this now, but I think this should work:
Code:
#define manage( val, code_block ) \
   { \
      bool managed = false; \
      try \
      { \
         while ( MyClass::manage( val, managed ) ) \
            code_block \
      } \
      catch (...) \
      { \
         MyClass::manage( val, managed ); \
      } \
   }

manage( someObj,
        { /* some code in here. */ } )
 
Thanks, cpjust, I'll give that a try. It's pretty close to what I'm looking for and I'll have to play around with it to see if I can modify it slightly in syntax.

Nice thinking! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top