Thread: exception handling wrapper functions?

  1. #16
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by cpjust View Post
    I don't think I'd want to use this in production code, but I've used it in a QA test harness before:
    Code:
    #define TRY( func, file, line ) \
        try { \
            func; \
        } catch (...) { \
            cout << "Error!  Exception caught in " << file << ", line: " << line << endl; \
        }
    
    int main()
    {
        TRY( someThrowingFunc(), __FILE__, __LINE__ )
    ...
    }
    I'm not sure if that's what you were looking for?


    yes, more or less...

    what i'm doing is iterating through a vector of functors, and i'd like to wrap the call to the functor's execution method in a try/catch block, so that any exceptions generated by the functor are caught by the functor container and then either handled, or at the very least a log entry is made detailing which functor generated the error so it can be corrected and/or a handler can be written for it in the next release.
    Last edited by m37h0d; 06-09-2008 at 02:50 PM.

  2. #17
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    allow it to propagate.
    Honestly? I thought that the propagation of thrown exceptions was bad coding practice.

    I have implemented some code with encapsulated exception handling, make some data structure (they exist in STL I believe) in which you store the message. At catch you can eventually add information, this keeps the thrown message (if you throw a message) as generic as possible.

    The dilemma between "if/else" or "exceptions" should not exist, exceptions should be used if just somewhere in a try block that block should be stopped from being executed. If/else would be impossible here...

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MarkZWEERS View Post
    Honestly? I thought that the propagation of thrown exceptions was bad coding practice.
    Exactly the opposite. If a functionA() calls function B(), functionB() throws an exception, and functionA() cannot rationally recover from exception (which means it cannot recover from the conditions that caused the exception), then the exception should be allowed to propagate.

    To answer the original it is possible to write a function that enables exception handling code, thus;
    Code:
    try
    {
        functionThatThrows();
    }
    catch(...)
    {
        Handler();
    }
    
    void Handler()     // called ONLY if an exception is active
    {
          try
          {
                 throw;              // rethrows the active exception.  abort()'s if no active exception
          }
          catch(Exception1 &)
          {
               // handle Exception1
          }
          catch (Exception2 &)
          {
               // handle Exception2
          }
           // etc
            
           
    }
    This doesn't completely eliminate the need for a try/catch block in every function that needs to do the same exception handling, but it does allow writing a series of handlers only once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 06:37 AM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  4. CIN and exception handling
    By OldSchool in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2006, 05:02 PM
  5. handling basic math functions
    By MyDestiny in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2005, 01:12 PM

Tags for this Thread