User-defined Exceptions: Name their own exceptions by creating a new exception class : Exception Class « Exception « Python






User-defined Exceptions: Name their own exceptions by creating a new exception class

User-defined Exceptions: Name their own exceptions by creating a new exception class


#Exceptions should typically be derived from the Exception class, either directly or 
#indirectly. For example:

class MyError(Exception):
     def __init__(self, value):
         self.value = value
     def __str__(self):
         return repr(self.value)
 
try:
     raise MyError(2*2)
except MyError, e:
     print 'My exception occurred, value:', e.value
 


           
       








Related examples in the same category

1.A programmer-defined exception class.A programmer-defined exception class.
2.Create a base class for exceptions defined by a module