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 replace '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 (EDIT: or BaseException) class, that only its family members can be raised?
No.
Typically, you inherit from Exception. Period. That’s what it says.
Sometimes, you might inherit from BaseException. That’s what it doesn’t say. You might extend BaseExcetion because you want to defeat
except Exceptionhandlers.They’re subclasses of
BaseException. What more do you need to know? The source is readily available. You can read the source code for theraisestatement to see exactly what it checks before it throws theTypeError.http://svn.python.org/view/python/trunk/Python/ceval.c?annotate=80817
Lines 3456 to 3563.
However, all that matters from a practical stand-point is “subclasses of
BaseException.”