GNU Info

Info Node: (python2.1-tut.info)User-defined Exceptions

(python2.1-tut.info)User-defined Exceptions


Next: Defining Clean-up Actions Prev: Raising Exceptions Up: Errors and Exceptions
Enter node , (file) or (file)node

User-defined Exceptions
=======================

Programs may name their own exceptions by assigning a string to a
variable or creating a new exception class.  For example:

     >>> class MyError:
     ...     def __init__(self, value):
     ...         self.value = value
     ...     def __str__(self):
     ...         return `self.value`
     ...
     >>> try:
     ...     raise MyError(2*2)
     ... except MyError, e:
     ...     print 'My exception occurred, value:', e.value
     ...
     My exception occurred, value: 4
     >>> raise MyError, 1
     Traceback (most recent call last):
       File "<stdin>", line 1
     __main__.MyError: 1

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

More information on classes is presented in chapter Note: Classes,
"Classes."


automatically generated by info2www version 1.2.2.9