I don't know what the rest of your code looks like, but I'm not sure I get exactly what you are trying to do (you seem to want to raise and handle your exception in the same place).You will need to define your own Exception class, and then a module that raises the exception if something goes wrong, then the place that calls that module must be within a try...except statement which handles the exception. Here is a simple example of the most trivial error raising program; which will catch some common errors when attempting to read a text file:import os
class fileLoaderErrors(Exception):
'''error handling; error code:
0 - path doesn't exist
1 - extension isn't correct
'''
def __init__(self, errorCode, filePath):
self.errorCode = errorCode
self.filePath = filePath
def __str__(self):
if self.errorCode == 0:
errorStr = 'File does not exist at specified path: %s' % self.filePath
elif self.errorCode == 1:
errorStr = 'File extension is not correct, .txt is required, file provided: %s' % self.filePath
else:
errorStr = 'Unknown error with %s' % self.filePath
return errorStr
def fileOpener(filePath):
'''stupid little module to load a file if it exists and the extension is correct, otherwise throw an exception'''
if os.path.exists(filePath):
if filePath.find('.txt') != -1:
return open(filePath)
else:
raise fileLoaderErrors(1, filePath)
else:
raise fileLoaderErrors(0, filePath)
if __name__ == "__main__":
path = 'D:\\Temp\\new2.txt'
try:
fObj = fileOpener(path)
except fileLoaderErrors: # if there is an error, couln't load file...
print 'Unable to load file: %s' % path
fObj = None
if fObj is not None: # if could open file, print contents and close...
for line in fObj:
print line
fObj.close()
Notice that because I am catching my custom exception from the module it doesn't matter what the error message was, the program will go on to do its thing. This, however, won't catch other exceptions (such as forgetting the import os line at the top of the script).Here is the code for what I think you are trying to do:class LockError(Exception):
def __init__(self, file):
self.file = file
def __str__(self):
return 'Error: could not obtain lock on %s' % (self.file)
class UnknownError(Exception):
def __init__(self, file):
self.file = file
def __str__(self):
return 'Error: %s does not exist %s' % (self.file)
def fileRenamer(TABLE_OWNER, SDE_FC_CURRENT, SDE_FC_BACKUP):
print "\n5. Now renaming " + TABLE_OWNER + SDE_FC_CURRENT + " to " + TABLE_OWNER + SDE_FC_BACKUP + " providing no lock exists"
if arcpy.Exists(TABLE_OWNER + SDE_FC_CURRENT) : # test if CURRENT exists
if arcpy.TestSchemaLock(TABLE_OWNER + SDE_FC_CURRENT) and arcpy.TestSchemaLock(TABLE_OWNER + SDE_FC_BACKUP) : # test locking both
arcpy.Rename_management (TABLE_OWNER + SDE_FC_CURRENT, TABLE_OWNER + SDE_FC_BACKUP) # perform rename
elif not arcpy.TestSchemaLock(TABLE_OWNER + SDE_FC_CURRENT) : # if CURRENT couldn't be locked, raise the exception
raise LockError(SDE_FC_CURRENT)
elif not arcpy.TestSchemaLock(TABLE_OWNER + SDE_FC_BACKUP) : # if it was BACKUP that couldn't be locked, raise the exception
raise LockError(SDE_FC_BACKUP)
elif not arcpy.Exists(TABLE_OWNER + SDE_FC_CURRENT) : # CURRENT doesn't exist, raise the exception
raise UnknownError(SDE_FC_CURRENT)
if __name__ == "__main__":
table_owner = '...'
sde_fc_current = '...'
sde_fc_backup = '...'
fileRenamer(table_owner, sde_fc_current, sde_fc_backup)
which will trow exceptions (displaying the relevant error messages). In reality you want to handle those exceptions as well, so you will need to replace the last line with these lines:try:
fileRenamer(table_owner, sde_fc_current, sde_fc_backup)
except LockError: # if it has the lock error
print 'Error obtaining lock on one of the data-sets during backup, attempting something else...'
# Doing something else goes here...
except UnknownError: # if it was the unknown error
print 'Error with CURRENT SDE - it does not exist'
# Do something else here, or whatever...
You may even want to create different exceptions, depending on if it is the CURRENT or BACKUP features which is locked...Let me know how you get on!