[Tutor] exception classes

dman dsh8290@rit.edu
Mon, 31 Dec 2001 17:27:55 -0500


On Mon, Dec 31, 2001 at 01:15:28PM -0600, Kevin McCormick wrote:
| I am trying to figure out how to design an exception class so that my 
| class / module can handle errors nicely.  What material I have found 
| doesn't really explain it for me.  Can anyone give a brief explanation 
| or directions to good explanations?

What does your exception need to be able to do and what info does it
need to store?  The following is a minimal example :

# first define the class
class MyException( Exception ) :
    pass


# now somewhere else we can raise it if need be
def my_func() :
    # this should only be done in case of an error,
    # in this example an error always occurs
    raise MyException()


# now use the function that might raise an exception, _and_ handle
# that situaiton
try :
    my_func()
except MyException , err :
    print "Whoa, an exception was raised"
    print err



HTH,
-D

-- 

A)bort, R)etry, B)ang it with a large hammer