How to catch str exception?

Kurt Symanzik kurt at kbsymanzik.org
Fri May 15 04:45:10 EDT 2009


"anuraguniyal at yahoo.com" <anuraguniyal at yahoo.com> wrote on 2009-05-15 
4:13:15 PM +0800
> 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

Try this, i.e. catch the exact string:

except "xxx":


Kurt

-- 
Kurt Symanzik
kurt at kbsymanzik.org
Skype id: ksymanzik
http://kbsymanzik.org



More information about the Python-list mailing list