I've been tasked with updating a number of scripts that we run over night as scheduled tasks. The original scripts use several try: except blocks that look like this:
if arcpy.Exists(gdbPathNameTestBAK):
try:
arcpy.Delete_management(gdbPathNameTestBAK)
print "Success: Deleted BACKUP " + gdbPathNameTestBAK
except Exception, ErrorDesc:
errorFlag = True
EmailNoticeDeleteBack = "Error: No Delete BACKUP " + gdbPathNameTestBAK
print str(ErrorDesc) + "Error: No Delete BACKUP " + gdbPathNameTestBAK
# note the except Exception, ErrorDesc:
EmailNoticeExportXML = ""
try:
arcpy.ExportXMLWorkspaceDocument_management(sdePathName, xmlWorkspacePath, "SCHEMA_ONLY", "BINARY", "NO_METADATA")
print "Success: Exported XML workspace to " + xmlWorkspacePath
except Exception, Err:
errorFlag = True
EmailNoticeExportXML = "Error: No Export XML workspace to " + xmlWorkspacePath
print str(Err) + " Error: No Export XML workspace to " + xmlWorkspacePath
#note the except Exception Err:
My question is in regards to the except arguments. What's the difference between ErrorDesc and Err? Also, I've changed the the print statements to :
###original --print str(ErrorDesc) + "Error: No Delete BACKUP " + gdbPathNameTestBAK
#mine:
print'{} Error: No Delete BACKUP'.format(gdbPathNameTestBAK)
### original -- print str(Err) + " Error: No Export XML workspace to " + xmlWorkspacePath
#mine:
print '{} Error: No Export XML workspace to'.format(xmlWorkspacePath)
After running the print statements I drop the str() method as it does not seem to make a difference. Thoughts there?
Solved! Go to Solution.
Joe... I think your ErrDesc is the equivalent to 'e' in this example (and in the python docs)
Difference between except: and except Exception as e: in Python - Stack Overflow
and fancy-schmancy format strings that you are now using, automatically convert anything that has a string representation to a string. Some objects … like our favorite... None will show as NoneType
Joe... I think your ErrDesc is the equivalent to 'e' in this example (and in the python docs)
Difference between except: and except Exception as e: in Python - Stack Overflow
and fancy-schmancy format strings that you are now using, automatically convert anything that has a string representation to a string. Some objects … like our favorite... None will show as NoneType
Hahaha- 'fancy-schmancy'?! I learned them from you!
As an after thought, these are all in Python 2.x scripts; I notice when I plug the the try-except blocks into Python 3.x in a Spyder IDE, it will complain if you don't use:
try:
#some code that may throw an exception
except Exception as e:
#exception handling code
#### Note the as e reference####