What am I doing wrong here?

Gerhard Häring gh at ghaering.de
Sun Jun 29 23:35:17 EDT 2003


Bill C. Wong wrote:

> ################################
> def func( s ):
>     s1, s2 = s.split( ' ', 1 )
>     try:
>         raise s1
>     except "aaaa":
>         print 'except "aaaa"'
>     except:
>         print 'except:'
> 
> s = "aaaa "
> func( s )
> ################################
> If I manually assign s1 with s1 = "aaaa", then it works fine! What am I
> doing wrong here?

Ok, looking closer this looks like your fault, not Python's:

Please read http://python.org/doc/current/ref/try.html

which states for the first parameter of the except clause:

"""Note that the object identities must match, i.e. it must be the same 
object, not just an object with the same value."""

The lesson learnt is to *not* use string exceptions. String exceptions 
are terrible outdated and quite frankly suck. If you want to create a 
custom exception, subclass Exception and catch this class.

-- Gerhard





More information about the Python-list mailing list