exception attributes

Frederic Giacometti fred at arakane.com
Sun Aug 19 18:43:27 CEST 2001


"J.Jacob" <joost_jacob at hotmail.com> wrote in message
news:13285ea2.0108190737.7a18c8b8 at posting.google.com...
> Hello,
>
> When testing exceptions i found something, maybe a bug ?
> I wonder if the behavior is intentionally.
> Hereby a small program to show it:

...

All exception classes must be subclasses of the Exception standard class
(see standard documentation, or use some common sense ;)).

Thus, the code is wrong in the first place...


> 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