Python: User-Defined Exception That Proves The Rule

Posted by bandana on Stack Overflow See other posts from Stack Overflow or by bandana
Published on 2010-05-08T10:59:15Z Indexed on 2010/05/08 11:08 UTC
Read the original article Hit count: 267

Filed under:
|

Python documentations states: Exceptions should typically be derived from the Exception class, either directly or indirectly.

the word 'typically' leaves me in an ambiguous state.

consider the code:

class good(Exception): pass
class bad(object): pass

Heaven = good()
Hell = bad()

>>> raise Heaven

Traceback (most recent call last):
  File "<pyshell#163>", line 1, in <module>
    raise Heaven
good

>>> raise Hell

Traceback (most recent call last):
  File "<pyshell#171>", line 1, in <module>
    raise Hell
TypeError: exceptions must be classes or instances, not bad

so when reading the python docs, should i change 'typically' with ''?

what if i have a class hierarchy that has nothing to do with the Exception class, and i want to 'raise' objects belonging to the hierarchy?

i can always raise an exception with an argument:

raise Exception, Hell

This seems slightly awkward to me

What's so special about the Exception class, that only its family members can be raised?

© Stack Overflow or respective owner

Related posts about python

Related posts about exception