Exception handling....dumb question?

Trent Mick trentm at ActiveState.com
Fri Mar 31 19:40:18 EST 2006


[kbperry wrote]
> In Python,
> When using the default except (like following)
> 
> try:
>     some code that might blow up
> 
> except:
>     print "some error message"


    >>> try:
    ...     1/0
    ... except:
    ...     import traceback
    ...     traceback.print_exc()
    ...
    Traceback (most recent call last):
      File "<stdin>", line 2, in ?
    ZeroDivisionError: integer division or modulo by zero


Or, if you are using the logging module:

    >>> import logging
    >>> log = logging.getLogger("myscript")
    >>> logging.basicConfig()
    >>>
    >>> try:
    ...     1/0
    ... except:
    ...     log.exception("whoa!")
    ...
    ERROR:myscript:whoa!
    Traceback (most recent call last):
      File "<stdin>", line 2, in ?
    ZeroDivisionError: integer division or modulo by zero


Cheers,
Trent

-- 
Trent Mick
TrentM at ActiveState.com



More information about the Python-list mailing list