try - except. How to identify errors unknown in advance?

Christian Heimes lists at cheimes.de
Wed Nov 16 13:47:45 EST 2011


Am 16.11.2011 19:39, schrieb Frederic Rentsch:
>> py>import sys
>> py>try:
>> py> raise RuntimeError
>> py> except:
>> py> print sys.exc_info()
>> py>
>> (<type 'exceptions.RuntimeError'>, RuntimeError(), <traceback object
>> at 0x0000000002371588>)
> 
> Chris, Thanks very much! Great help!

How about using the excellent logging framework instead of rolling your
own stuff? It can print the traceback, too.

>>> import logging
>>> logging.basicConfig()
>>> log = logging.getLogger("mymodule")
>>> try:
...     raise ValueError("test")
... except Exception:
...     log.exception("some message")
...
ERROR:mymodule:some message
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: test

Christian




More information about the Python-list mailing list