Copyright (C) 2000-2012 |
GNU Info (python2.1-tut.info)User-defined ExceptionsUser-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 |