how can I put an Exception as the key of a hash table

7stud bbxx789_05ss at yahoo.com
Sat Apr 28 16:05:36 EDT 2007


yinglcs at gmail.com wrote:
> I want to keep track of the number of different exception happens in
> my python program:
>
> ErrorHash = {}
>
> try:
>
>  # come code ...
>
>  except Exception, e:
>             print e
>             errcode = e
>
>             if (ErrorHash.has_key(errcode)):
>                 ErrorFailNo = ErrorHash[errcode]
>
>                 ErrorHash[errcode] = ErrorFailNo + 1
>
>             else:
>                 ErrorHash[errcode] = 1
>
>
> But when i print out the ErrorHash like this:
>
> print ErrorHash
>
> i get an empty string.  Can you please tell me how can I put an
> Exception as the key of a hash table ? Or how can i dump out all the
> content of the hashtable.
>
> Thank you.

Apparently, the Exception class's __str__() method doesn't print
anything about the exception.  That doesn't mean the exception is an
empty string though:


ErrorHash = {}

try:

    raise ValueError

except Exception, e:
    print "the exception is:", e, "<----"

    if (ErrorHash.has_key(e)):
        ErrorFailNo = ErrorHash[e]
        ErrorHash[e] = ErrorFailNo + 1
    else:
        ErrorHash[e] = 1

print ErrorHash




More information about the Python-list mailing list