How to catch str exception?

Dave Angel davea at ieee.org
Fri May 15 09:12:39 EDT 2009


anuraguniyal at yahoo.com wrote:
> import sys
> try:
>     raise "xxx"
> except str,e:
>     print "1",e # is not caught here
> except:# is caught here
>     print "2",sys.exc_type,sys.exc_value
>
> In the above code a string exception is raised which though deprecated
> but still a 3rd party library I use uses it. So how can I catch such
> exception without relying on catch all, which could be bad.
>
> system: Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3
> (Ubuntu 4.2.3-2ubuntu7)] on linux2
>
>   
Just fix the except type to be the exception caused by the illegal "raise"

except TypeError,e:
    print "1",e # is caught here


1 exceptions must be classes or instances, not str




More information about the Python-list mailing list