Can I change the behaviour of "raise" or "Exception"?
<p>My project's code is full of blocks like the following:</p>

<pre><code>try:
    execute_some_code()
except Exception:
    print(datetime.datetime.now())
    raise
</code></pre>

<p>simply because, if I get an error message, I'd like to know when it happened. I find it rather silly to repeat this code over and over, and I'd like to factor it away.</p>

<p>I don't want to decorate <code>execute_some_code</code> with something that does the error capturing (because sometimes it's just a block of code rather than a function call, and sometimes I don't need the exact same function to be decorated like that). I also don't want to divert stdout to some different stream that logs everything, because that would affect every other thing that gets sent to stdout as well.</p>

<p>Ideally, I'd like to over-ride the behaviour of either the <code>raise</code> statement (to also print <code>datetime.datetime.now()</code> on every execution) or the <code>Exception</code> class, to pre-pend all of its messages with the time. I can easily sub-class from <code>Exception</code>, but then I'd have to make sure my functions raise an instance of this subclass, and I'd have just as much code duplication as currently.</p>

<p>Is either of these options possible?</p>