Exceptions' Behaviour

Martin Sjögren martin at strakt.com
Wed Jul 18 08:26:38 EDT 2001


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

-- 
Martin Sjögren
  martin at strakt.com              ICQ : 41245059
  Phone: +46 (0)31 405242        Cell: +46 (0)739 169191
  GPG key: http://www.strakt.com/~martin/gpg.html




More information about the Python-list mailing list