Freitag, 3. Juli 2009

PYTHON: get your own exception catcher

you can set your own exception handler, this is done by
sys.excepthook = your_func


for that you must define a function, with following parameters:

def your_func(exc_type, exc_obj, exc_tb):
print "whoaaa my exception"


then you can call an exception

5/0
=>
whoaaa my exception


def your_func(exc_type, exc_obj, exc_tb):
import traceback
l = traceback.format_exception(exc_type, exc_obj, exc_tb)
print type(l)
print l


=>

['Traceback (most recent call last):\n', ' File "./t1.py", line 37, in \n 5/0\n', 'ZeroDivisionError: integer division or modulo by zero\n']


traceback.format_exception(...) return us a list of information
as a normal exception traceback


info about sys.xxx

Keine Kommentare:

Kommentar veröffentlichen