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

jestes39 at yahoo.com jestes39 at yahoo.com
Tue Jul 3 10:54:47 EDT 2001


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