[Tutor] exception classes

alan.gauld@bt.com alan.gauld@bt.com
Tue, 1 Jan 2002 19:17:34 -0000


> I have seen this where a block goes something like:
> 
> try:
>     <some operation>
> except:
>     raise MyException( 'error in some operation' )

Thats kind of uncommon. Norally you raise exceptions 
in your code and somebody else catches them. So:

class BadArg(Exception): pass

def MyFunct(arg):
    if not arg:
       raise BadArg('fooey!')
    else: # do the deed

Now in a program a client goes:

    try:
       MyFunc(None)
    except BadArg: 
       print 'oops!'

> Now, what do you do with it?

Apart from Exception classes(as opposed to strings) 
I explain exception handling in my web tutor. Have a 
look and see if it helps.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld