1

I'm initialize 2 servers in one twisted script. So I want to get all unhadled exception inside of it on my mail. Is it posible?

Thank you!

Oduvan
  • 2,607
  • 3
  • 24
  • 24

1 Answers1

2

You can use sys.excepthook. sys.excepthook catches all exceptions that are uncaught in your code. How you handle them from there is your choice.

For sending emails, you may be interested in the Python email module. Here are some examples.

You can reconstruct a traceback from an exception using the Python traceback module; specifically format_exception().

import sys
import traceback

def excepthook(type_, value, tb):
    # do whatever with the exception information
    # you want to do here

    exception = traceback.format_exception(type_, value, tb)
    print ''.join(exception)

sys.excepthook = excepthook

Note: I don't know anything about twisted, so there may be a twisted way (pun not intended) to do this, however, this is how I would do it if there was no other official way of course.

John Doe
  • 3,436
  • 1
  • 18
  • 21
  • Thank. It's a good idea. I'll test today and let you know. Thanks. – Oduvan Oct 27 '11 at 11:57
  • How're you doing it? Any code samples which demonstrate the issue? Are you sure that the code itself isn't at fault, vs this way of doing it? – John Doe Oct 28 '11 at 19:24