Python determine whether an exception was thrown (regardless of whether it is caught or not) <p>I am writing tests for some legacy code that is littered with catch-all constructs like</p> <pre><code>try: do_something() do_something_else() for x in some_huge_list(): do_more_things() except Exception: pass </code></pre> <p>and I want to tell whether an exception was thrown inside the try block.</p> <p>I want to avoid introducing changes into the codebase just to support a few tests and I don't want to make the <code>except</code> cases more specific for fear of unintentionally introducing regressions. </p> <p>Is there a way of extracting information about exceptions that were raised and subsequently handled from the runtime? Or some function with a similar API to <code>eval</code>/<code>exec</code>/<code>apply</code>/<code>call</code> that either records information on every raised exception, lets the user supply an exception handler that gets run first, or lets the user register a callback that gets run on events like an exception being raised or caught.</p> <p>If there isn't a way to detect whether an exception was thrown without getting under the (C)Python runtime in a really nasty way, what are some good strategies for testing code with catch-all exceptions inside the units you're testing?</p>