Exception handling....dumb question?

Ben Finney bignose+hates-spam at benfinney.id.au
Sat Apr 1 01:04:20 EST 2006


"kbperry" <keith.b.perry at gmail.com> writes:

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

This will catch *every* exception, and throw it away before it gets to
your "print" statement.

This is almost never a good idea. You should catch *specific*
exceptions that you know you can deal with at that point in the code.

    import logging
    try:
        foo = 12 / 0
    except ZeroDivisionError, e:
        print "You *knew* this was going to happen: '%s'" % e
        logging.error(str(e))

This allows all other exceptions to propogate back through the call
stack.

More information on 'try':

    <URL:http://docs.python.org/ref/try.html>

-- 
 \          "When we call others dogmatic, what we really object to is |
  `\        their holding dogmas that are different from our own."  -- |
_o__)                                                   Charles Issawi |
Ben Finney




More information about the Python-list mailing list