Python Code Auditing Tool

Skip Montanaro skip at mojam.com
Wed Feb 2 13:45:35 EST 2005


    >> 246 ValueError
    >> 227 aetools.Error
    >> 216 Error
    >> 124 TypeError
    >> 101 error
    >> 75 RuntimeError
    >> 53 IOError
    >> 36 NotImplementedError
    >> 36 ImportError
    >> 36 EOFError
    >> 31 SyntaxError
    >> 23 KeyError
    >> 23 AttributeError
    >> 22 DistutilsPlatformError
    >> 21 UnicodeError

    Roy> It's kind of interesting (scarry?) that in roughly 20% of the cases
    Roy> nothing more specific than Error is raised.

Not really.  You might have code in a module named mod like this:

    class Error(Exception): pass

then later:

    def foo(...):
        ... blah blah blah ...
        if condition:
            raise Error, "hey dummy!"

The caller might look like:

    import mod

    ...

    try:
        mod.foo()
    except mod.Error, msg:
        print msg

That said, the tendency for many newer modules seems to be to discriminate
exceptions based on type (look at urllib2 for example) while older modules
tended to have just a single exception they raised (look at ftplib).  I
suspect that has something to do with whether the module was originally
written before or after Python introduced class exceptions.

Skip



More information about the Python-list mailing list