Print out exception info : Exception Info « Exception « Python






Print out exception info

Print out exception info

#the exception instance defines __getitem__ and __str__ so the arguments can be 
#accessed or printed directly without having to reference .args.

try:
    raise Exception('spam', 'eggs')
except Exception, inst:
    print type(inst)     # the exception instance
    print inst.args      # arguments stored in .args
    print inst           # __str__ allows args to printed directly
    x, y = inst          # __getitem__ allows args to be unpacked directly
    print 'x =', x
    print 'y =', y


          
       








Related examples in the same category

1.Output exception arguments, string representation of exception,and the tracebackOutput exception arguments, string representation of exception,and the traceback
2.Get an exception's argumentGet an exception's argument