8.5 User-defined Exceptions

Programs may name their own exceptions by assigning a string to a variable. For example:

>>> my_exc = 'my_exc'
>>> try:
...     raise my_exc, 2*2
... except my_exc, val:
...     print 'My exception occurred, value:', val
... 
My exception occurred, value: 4
>>> raise my_exc, 1
Traceback (innermost last):
  File "<stdin>", line 1
my_exc: 1

Many standard modules use this to report errors that may occur in functions they define.


guido@python.org