exception handing
Sean 'Shaleh' Perry
shalehperry at attbi.com
Sat Jun 29 11:58:00 EDT 2002
On 29-Jun-2002 Rhymes wrote:
> I'm reading "Learning Python" and in the section "gotchas"
> of the exception handling chapter the book itself states
> that the exception matching is made by identity not equality.
> It also write an example such as this:
>
>>>> ex1 = "spam"
>>>> ex2 = "spam"
>>>>
>>>> ex1 == ex2, ex1 is ex2
> (1, 0)
>
> <--- here i get (1, 1) ---->
>
looks like the interpreter is now optimizing shared strings.
>>>> try:
> ... raise ex1
> ... except ex1:
> ... print 'got it'
> ...
> got it
>>>> try:
> ... raise ex1
> ... except ex2:
> ... print 'Got it'
> ...
> Traceback (innermost last):
> File "<stdin>", line 2, in ?
> spam
>
> but in my Python 2.2 i don't get back any error from the interpreter
> about the second example... why? What has changed (since 1.5 release)
> about objects identity?
>
modern python uses exception classes and not strings.
>>> class BadThingsException: pass
...
>>> try:
... raise BadThingsException
... except BadThingsException:
... print 'got it'
...
got it
More information about the Python-list
mailing list