exception attributes

J.Jacob joost_jacob at hotmail.com
Sun Aug 19 17:37:04 CEST 2001


Hello,

When testing exceptions i found something, maybe a bug ?
I wonder if the behavior is intentionally.
Hereby a small program to show it:

---code starts after this line---------------------------------------------
"""

See code below:
sys.exc_value.name receives the class attribute called 'name' unless there
also is an instance attribute called 'name'...
...the class attribute is now invisible...  


"""


class MyError:
    def __init__(self):
        self.name = 'MyError instance attribute name'
        MyError.name = 'MyError class attribute name'


class MyError2:
    def __init__(self):
        MyError2.name = 'MyError2 class attribute name'


def oops():
    raise MyError


def oops2():
    raise MyError2


import sys

def oopsCaller():
    try:
        oops()
    except MyError:
        print 'name attribute value:', sys.exc_value.name
    else:
        print 'no catch in oopsCaller()'

def oopsCaller2():
    try:
        oops2()
    except MyError2:
        print 'name attribute value:', sys.exc_value.name
    else:
        print 'niks gevangen in oopCaller2()'

    

if __name__ == '__main__':
    oopsCaller()
    oopsCaller2()



More information about the Python-list mailing list