<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Error handling with Python script tools in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682293#M52837</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the clarifications and the link.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 26 Jul 2011 13:53:56 GMT</pubDate>
    <dc:creator>AliceDeschamps1</dc:creator>
    <dc:date>2011-07-26T13:53:56Z</dc:date>
    <item>
      <title>Error handling with Python script tools</title>
      <link>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682291#M52835</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to integrate some error handling in my python script tools but I am not exactly sure how to do it properly.&amp;nbsp; The desktop help has a lot of great examples but there are so many different ways of doing this.&amp;nbsp;&amp;nbsp; I am also a bit confused with the difference between error messaging in standalone script vs script tools??&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Below is a subset of my script, only showing the error handling portion.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
import sys, string, os
arcpy.env.overwriteOutput = True

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Check out any necessary licenses
&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.CheckProduct("ArcInfo") == "Unavailable":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("ArcInfo Licensing Issue")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise LicenseError
&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.CheckExtension("Spatial") == "Available":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CheckOutExtension("Spatial")
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Spatial Analyst Licensing Issue")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise LicenseError

# all all the processing here....


except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages(2)
finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Check in the Spatial Analyst extension
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CheckInExtension("Spatial")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Questions:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-Will the above work properly for script tools?&amp;nbsp; For the &lt;/SPAN&gt;&lt;STRONG&gt;except: block&lt;/STRONG&gt;&lt;SPAN&gt; will the print do anything in script tools or do I need to use some other syntax to print the error instead?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-In this particular tool I have a cleanup routine that deletes intermediate files.&amp;nbsp; I currently have this before the except statement but would like to make sure that if the tool crashes that it cleans-up the intermediate files.&amp;nbsp; Is it possible to put the cleanup routine within the &lt;/SPAN&gt;&lt;STRONG&gt;finally: block&lt;/STRONG&gt;&lt;SPAN&gt;?&amp;nbsp; Is that good coding practice?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any tips on simple and best practice for error handling in script tools (containing a series of geoprocessing steps) would be greatly appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Alice&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:42:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682291#M52835</guid>
      <dc:creator>AliceDeschamps1</dc:creator>
      <dc:date>2021-12-12T04:42:15Z</dc:date>
    </item>
    <item>
      <title>Re: Error handling with Python script tools</title>
      <link>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682292#M52836</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Take a look at this Geoprocessing Blog post: &lt;/SPAN&gt;&lt;A href="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2008/12/01/Tips-and-tricks-_2D00_-Error-handling-in-Python-script-tools.aspx" rel="nofollow noopener noreferrer" target="_blank"&gt;http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2008/12/01/Tips-and-tricks-_2D00_-Error-handling-in-Python-script-tools.aspx&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With regards to the print command, it will not work in script tools, you need to use arcpy.AddMessage/AddWarning/AddError instead. What I usually do is have functions for messages, warnings, and errors, that does both. E.g.:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def message(msg):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print msg
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage(msg)

def warning(msg):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print msg
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddWarning(msg)

def error(msg):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print msg
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddError(msg)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just switch out the gp for arcpy since you're using that. There may be better ways to do this (e.g. the logging module as suggested here: &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/4719-Dumb-question-can-I-write-standalone-python-script-to-run-without-arcgis?p=51794&amp;amp;viewfull=1#post51794" rel="nofollow noopener noreferrer" target="_blank"&gt;http://forums.arcgis.com/threads/4719-Dumb-question-can-I-write-standalone-python-script-to-run-without-arcgis?p=51794&amp;amp;viewfull=1#post51794&lt;/A&gt;&lt;SPAN&gt;) but this is simple enough for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And yes, the finally clause is the best place to put cleanup code that is to be executed whether an error occurs or not.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:42:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682292#M52836</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2021-12-12T04:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Error handling with Python script tools</title>
      <link>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682293#M52837</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the clarifications and the link.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jul 2011 13:53:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682293#M52837</guid>
      <dc:creator>AliceDeschamps1</dc:creator>
      <dc:date>2011-07-26T13:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: Error handling with Python script tools</title>
      <link>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682294#M52838</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I find the traceback functionality very useful and I adapted the scripts examples from the suggested blog link below for ArcGISv10.&amp;nbsp; I included a snippet of the modified working script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://blogs.esri.com/dev/blogs/geoprocessing/archive/2008/12/01/tips-and-tricks-_2d00_-error-handling-in-python-script-tools.aspx" rel="nofollow noopener noreferrer" target="_blank"&gt;http://blogs.esri.com/dev/blogs/geoprocessing/archive/2008/12/01/tips-and-tricks-_2d00_-error-handling-in-python-script-tools.aspx&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I noticed in that the blog uses the "def trace():"&amp;nbsp; inside (tip#4) or outside (tip#5) the try block?&amp;nbsp;&amp;nbsp; What is the logic for putting it inside vs. outside?&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Import system module
from xml.etree import ElementTree
import arcpy, string, os

def trace():
&amp;nbsp;&amp;nbsp;&amp;nbsp; import sys, traceback
&amp;nbsp;&amp;nbsp;&amp;nbsp; tb = sys.exc_info()[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(tb)[0] 
&amp;nbsp;&amp;nbsp;&amp;nbsp; line = tbinfo.split(", ")[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; filename = sys.path[0] + os.sep + "test.py"
&amp;nbsp;&amp;nbsp;&amp;nbsp; synerror = traceback.format_exc().splitlines()[-1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; return line, filename, synerror

if __name__ == '__main__':


&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inShapeFile= arcpy.GetParameterAsText(0) #final edited polygon shapefile
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inFile=arcpy.GetParameterAsText(1) #for R1 is .txt, for R2 is .xml
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; polList=str(arcpy.GetParameterAsText(2))#user select from drop list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # all the processing code goes here.....

&amp;nbsp;&amp;nbsp; except arcpy.ExecuteError:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Return Geoprocessing tool specific errors
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line, filename, err = trace()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Geoprocessing error on " + line + " of " + filename + " :")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for msg in range(0, arcpy.GetMessageCount()):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.GetSeverity(msg) == 2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddReturnMessage(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Gets non-tool errors
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line, filename, err = trace()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Python error on " + line + " of " + filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(err)&amp;nbsp; 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:42:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682294#M52838</guid>
      <dc:creator>AliceDeschamps1</dc:creator>
      <dc:date>2021-12-12T04:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Error handling with Python script tools</title>
      <link>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682295#M52839</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;I noticed in that the blog uses the "def trace():"&amp;nbsp; inside (tip#4) or outside (tip#5) the try block?&amp;nbsp;&amp;nbsp; What is the logic for putting it inside vs. outside?&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That's a good question, I hadn't noticed that. I'm not sure, but it looks like a logic error on the author's part. Normally wouldn't want to define a function inside of a try.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Aug 2011 19:40:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-handling-with-python-script-tools/m-p/682295#M52839</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2011-08-03T19:40:58Z</dc:date>
    </item>
  </channel>
</rss>

