title: Is it possible to capture all warnings raised in a try block in python?
body: <p>I have some code that's schematically along the lines of:</p>
  
  <pre><code>from logging import warning
  
  def complex_function():
      # Do some stuff
      raise Warning("blah") 
      # Do some more stuff
      raise Warning("Blah again") 
  
  try: 
      complex_function()
  except Warning as e: 
      warning(e) 
  </code></pre>
  
  <p>This results in:</p>
  
  <pre><code>WARNING:root:blah
  </code></pre>
  
  <p>I would like to catch all warnings raised, and log them. In my code, such warnings sometimes come from 3rd party libraries, so it is not practical to modify the warnings in place to use <code>logging.warning</code>, and I also want to store the warning information so that I can return some version of that information via an API.</p>
  
  <p>Is there a way for me to do something like this that catches all warnings, and loops over them?</p>
  
  <h2>edit</h2>
  
  <p>Too late, I'm realising that I'm raising warnings wrong in the example above, and <code>complex_function</code> should be something long the lines of:</p>
  
  <pre><code>def complex_function():
      # Do some stuff
      warnings.warn("blah") 
      # Do some more stuff
      warnings.warn("Blah again", UnknownWarningType)
  </code></pre>
  
  <p>And I think I can catch these with <code>warnings.catch_warnings</code></p>