# Import arcpy module import arcpy from arcpy import env from arcpy.sa import * import sys, string, os arcpy.env.overwriteOutput = True try: # Check out any necessary licenses if arcpy.CheckProduct("ArcInfo") == "Unavailable": arcpy.AddError("ArcInfo Licensing Issue") raise LicenseError if arcpy.CheckExtension("Spatial") == "Available": arcpy.CheckOutExtension("Spatial") else: arcpy.AddError("Spatial Analyst Licensing Issue") raise LicenseError # all all the processing here.... except: print arcpy.GetMessages(2) finally: # Check in the Spatial Analyst extension arcpy.CheckInExtension("Spatial")
I noticed in that the blog uses the "def trace():" inside (tip#4) or outside (tip#5) the try block? What is the logic for putting it inside vs. outside?
# Import system module from xml.etree import ElementTree import arcpy, string, os def trace(): import sys, traceback tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] line = tbinfo.split(", ")[1] filename = sys.path[0] + os.sep + "test.py" synerror = traceback.format_exc().splitlines()[-1] return line, filename, synerror if __name__ == '__main__': try: inShapeFile= arcpy.GetParameterAsText(0) #final edited polygon shapefile inFile=arcpy.GetParameterAsText(1) #for R1 is .txt, for R2 is .xml polList=str(arcpy.GetParameterAsText(2))#user select from drop list # all the processing code goes here..... except arcpy.ExecuteError: #Return Geoprocessing tool specific errors line, filename, err = trace() arcpy.AddError("Geoprocessing error on " + line + " of " + filename + " :") for msg in range(0, arcpy.GetMessageCount()): if arcpy.GetSeverity(msg) == 2: arcpy.AddReturnMessage(msg) except: #Gets non-tool errors line, filename, err = trace() arcpy.AddError("Python error on " + line + " of " + filename) arcpy.AddError(err)
def message(msg): print msg gp.AddMessage(msg) def warning(msg): print msg gp.AddWarning(msg) def error(msg): print msg gp.AddError(msg)
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.