title: wrapping class method in try / except using decorator
body: <p>I have a general purpose function that sends info about exceptions to an application log.
  I use the <code>exception_handler</code> function from within methods in classes.  The app log handler that is passed into and called by the <code>exception_handler</code> creates a JSON string that is what actually gets sent to the logfile.  This all works fine.</p>
  
  <pre><code>def exception_handler(log, terminate=False):
      exc_type, exc_value, exc_tb = sys.exc_info()
      filename, line_num, func_name, text = traceback.extract_tb(exc_tb)[-1]
      log.error('{0} Thrown from module: {1} in {2} at line: {3} ({4})'.format(exc_value, filename, func_name, line_num, text))
      del (filename, line_num, func_name, text)
      if terminate:
          sys.exit()
  </code></pre>
  
  <p>I use it as follows: (a hyper-simplified example)</p>
  
  <pre><code>from utils import exception_handler
  
  class Demo1(object):
      def __init__(self):
          self.log = {a class that implements the application log}
  
      def demo(self, name):
          try:
              print(name)
          except Exception:
              exception_handler(self.log, True)
  </code></pre>
  
  <p>I would like to alter <code>exception_handler</code> for use as a decorator for a large number of methods, i.e.:</p>
  
  <pre><code>@handle_exceptions
  def func1(self, name)
      {some code that gets wrapped in a try / except by the decorator}
  </code></pre>
  
  <p>I've looked at a number of articles about decorators, but I haven't yet figured out how to implement what I want to do. I need to pass a reference to the active log object and also pass 0 or more arguments to the wrapped function. I'd be happy to convert <code>exception_handler</code> to a method in a class if that makes things easier.</p>