Calling a user-defined Exception from within a function. Need Help.

Steve Holden sholden at holdenweb.com
Tue Jul 3 11:17:48 EDT 2001


Jason:

You don't say what error you are seeing. I think that your errors should be
in scope, and that therefore your code is fundamentally correct. However,
the way they are written, if you raise one then Python will try to set args
to NONE, which will in turn give you a NameError. Try replacing that with
None, and you might find things work out a little better.

Also note that it's a bit dangerous to use a catch-all "except:" as your
receipt() function does. You should really only catch the errors you are
expecting, allowing things like KeyboardInterrupt and SystemExit (?) to rise
up to the top level and either be handled by your main program or the
interpreter. But the basics appear correct.

Hope this helps.

regards
 Steve
--
http://www.holdenweb.com/


<jestes39 at yahoo.com> wrote in message
news:mailman.994172112.17718.python-list at python.org...
> I am still learning Python basics. I am trying to raise exceptions
> within a function. The problem is that my named exceptions aren't
> being recognized. They were working before I split the code up into
> modules. I just need to know where I need to define the exceptions or
> if I have to do something special to get them to work. Here's the
> basic shell of the code:
>
>   import sys
>   import telnetlib
>   import dbi,odbc
>   import exceptions
>
>   class database_error(exceptions.Exception):
>       def __init__(self,args=NONE):
>           self.args = args
>
>   class other(exceptions.Exception):
>       def __init__(self,args=NONE):
>           self.args = args
>
>   def parse(record):
>       try:
>           #Parse the row into the variables that will be used to
>           #  populate the receiving fields
>           .....
>           .....
>           return 'TRUE'
>       except:
>           return 'FALSE'
>
>   def receipt(record):
>       dbc = odbc.odbc(conn_str)
>       crsr = dbc.cursor()
>       try:
>           try:
>               if parse(record) == 'FALSE':
>                   raise other
>               tn = cas_login.login(username,password,host)
>
>               try:
>                   sql = "DELETE <table_name> WHERE ROWID = '" + rowid
> + "';"
>                   crsr.execute(sql)
>               except:
>                   raise database_error
>
>       except database_error:
>           dbc = None
>           crsr = None
>           tn.close()
>           print 'we made it to the database exception'
>       except other:
>           dbc = None
>           crsr = None
>           tn.close()
>           print 'unidentified error'
>       except:
>           dbc = None
>           crsr = None
>           tn.close()
>           print 'unhandled exception'
>
> Any help or pointers would be greatly appreciated. Thanks.
>
> Jason
>
>





More information about the Python-list mailing list