Exceptions' Behaviour
Tom Good
Tom_Good1 at excite.com
Wed Jul 18 14:58:43 EDT 2001
Martin Sj?ren <martin at strakt.com> wrote in message news:<mailman.995459309.19355.python-list at python.org>...
> I've been fooling around with exceptions a bit today, and found some
> things that I don't quite understand.
>
> class A (Exception): pass
> class B (Exception): pass
>
> try:
> raise A, B
> except A, var:
> print "A,", var
> except (A, B), var:
> print "(A,B),", var
>
> This prints 'A, main .B'
>
> try:
> raise A, B
> except (A, B), var:
> print "(A,B),", var
> except A, var:
> print "A,", var
>
> This prints '(A, B), main .B'
>
>
> That's rather predictiable, here comes the funky part:
>
> try:
> raise (A, B)
> except A, var:
> print "A,", var
> except (A, B), var:
> print "(A,B),", var
>
> This prints 'A, '
>
> try:
> raise (A, B)
> except (A, B), var:
> print "(A,B),", var
> except A, var:
> print "A,", var
>
> This prints '(A,B), '
>
> !!!
> What's going on here? If (A, B) is raised like A, B, why don't I get
> anything in my 'var' variable? If not, how come (A, B) is matched against
> A?
>
> I'm really confused here...
>
> Martin Sjögren
Consider this:
PythonWin 2.1 (#15, Jun 15 2001, 14:13:47) [MSC 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (MarkH at ActiveState.com) -
see 'Help/About PythonWin' for further copyright information.
>>> # raise A,B raises A with B as its argument
>>> raise "a", "b"
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
a: b
>>> #raise (A, B) raises A and ignores B
>>> raise ("a", "b")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
a
Keep in mind also, that a statement like:
except (A, B):
means to catch either A or B. That is, it does NOT mean to catch the
tuple (A,B).
Tom
More information about the Python-list
mailing list