title: How to determine if an exception was raised once you're in the finally block?
body: <p>Is it possible to tell if there was an exception once you're in the <code>finally</code> clause?  Something like:</p>
  
  <pre><code>try:
      funky code
  finally:
      if ???:
          print('the funky code raised')
  </code></pre>
  
  <p>I'm looking to make something like this more DRY:</p>
  
  <pre><code>try:
      funky code
  except HandleThis:
      # handle it
      raised = True
  except DontHandleThis:
      raised = True
      raise
  else:
      raised = False
  finally:
      logger.info('funky code raised %s', raised)
  </code></pre>
  
  <p>I don't like that it requires to catch an exception, which you don't intend to handle, just to set a flag.</p>
  
  <hr>
  
  <p>Since some <a href="https://stackoverflow.com/questions/49099637/how-to-determine-if-an-exception-was-raised-once-youre-in-the-finally-block#comment85224289_49099637">comments</a> are asking for less "M" in the MCVE, here is some more background on the use-case.  The actual problem is about escalation of logging levels.  </p>
  
  <ul>
  <li>The funky code is third party and can't be changed.</li>
  <li>The failure exception and stack trace does not contain any useful diagnostic information, so using <code>logger.exception</code> in an except block is not helpful here.</li>
  <li>If the funky code raised then some information which I need to see has already been logged, at level DEBUG.  We do not and can not handle the error, but want to escalate the DEBUG logging because the information needed is in there.</li>
  <li>The funky code does not raise, most of the time.  I don't want to escalate logging levels for the general case, because it is too verbose.  </li>
  </ul>
  
  <p>Hence, the code runs under a log capture context (which sets up custom handlers to intercept log records) and some debug info gets re-logged retrospectively:</p>
  
  <pre><code>try:
      with LogCapture() as log:
          funky_code()  # &lt;-- third party badness
  finally:
      # log events are buffered in memory. if there was an exception,
      # emit everything that was captured at a WARNING level
      for record in log.captured:
          if &lt;there was an exception&gt;:
              log_fn = mylogger.warning
          else:
              log_fn = getattr(mylogger, record.levelname.lower())
          log_fn(record.msg, record.args)
  </code></pre>