<?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: Python script tool function error handling in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-script-tool-function-error-handling/m-p/1045511#M60735</link>
    <description>&lt;P&gt;I was so excited because this is finally the question that should get the answer I need. I want my tool to stop right where it is, but it keeps going past the error. Maybe 6 years later someone will answer?&lt;/P&gt;</description>
    <pubDate>Fri, 09 Apr 2021 17:08:24 GMT</pubDate>
    <dc:creator>SarahSchwarzer1</dc:creator>
    <dc:date>2021-04-09T17:08:24Z</dc:date>
    <item>
      <title>Python script tool function error handling</title>
      <link>https://community.esri.com/t5/python-questions/python-script-tool-function-error-handling/m-p/93795#M7319</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Looking for advice and discussion on implementing script tools.&lt;/P&gt;&lt;P&gt;I have been adopting the convention where my script tools are embedded a functions. Here is my generic template:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
import sys
import traceback
import arcpy
from arcpy import env

class MsgError(Exception):
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass

def MyTool(args):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Enter python docstring here """
&amp;nbsp;&amp;nbsp;&amp;nbsp; # init cursor variables
&amp;nbsp;&amp;nbsp;&amp;nbsp; Row, Rows = [None] * 2
&amp;nbsp;&amp;nbsp;&amp;nbsp; # init temp file variables
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr1,tmp1 = [None] * 2
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # tool code here
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; except MsgError as xmsg:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(xmsg))
&amp;nbsp;&amp;nbsp;&amp;nbsp; except arcpy.ExecuteError:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(sys.exc_info()[2])[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(tbinfo.strip())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numMsg = arcpy.GetMessageCount()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(0, numMsg):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddReturnMessage(i)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as xmsg:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(sys.exc_info()[2])[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(tbinfo + str(xmsg))
&amp;nbsp;&amp;nbsp;&amp;nbsp; finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # temp files
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for f in [lyr1, tmp1]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if f:
&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; try:
&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(f)
&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; except:
&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass


if __name__ == "__main__":
&amp;nbsp;&amp;nbsp;&amp;nbsp; # ArcGIS Script tool interface
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get arguments, call tool
&amp;nbsp;&amp;nbsp;&amp;nbsp; argv = tuple(arcpy.GetParameterAsText(i)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(arcpy.GetArgumentCount()))
&amp;nbsp;&amp;nbsp;&amp;nbsp; MyTool(*argv)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The issue I'm running into is when I use this tool in another script and import it directly instead of going through ImportToolbox (for performance reasons), for example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from rastertools import MyTool
...
MyTool(arg1, arg2)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And the tool fails with an error, my calling script does not stop because i handled it. I want my calling script to bail in this situation.&lt;/P&gt;&lt;P&gt;Is the following approach valid? I'm assuming that if MyTool failed I will get a response from arcpy.GetMessages(2) since I called arcpy.AddError() in the called function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;MyTool(arg1, arg2)
if arcpy.GetMessages(2):
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise Exception("Geoprocessing tool MyTool failed")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Should I be using ImportToolbox to get my script tool instead of this more direct import of my tool function?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:35:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-tool-function-error-handling/m-p/93795#M7319</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-10T23:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: Python script tool function error handling</title>
      <link>https://community.esri.com/t5/python-questions/python-script-tool-function-error-handling/m-p/93796#M7320</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No response.... any ideas? Should I always import tools using arcpy.ImportToolbox to properly handle tool failure? I think I was avoiding this in the past before tools ran in process.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Mar 2015 14:55:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-tool-function-error-handling/m-p/93796#M7320</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2015-03-12T14:55:24Z</dc:date>
    </item>
    <item>
      <title>Re: Python script tool function error handling</title>
      <link>https://community.esri.com/t5/python-questions/python-script-tool-function-error-handling/m-p/1045511#M60735</link>
      <description>&lt;P&gt;I was so excited because this is finally the question that should get the answer I need. I want my tool to stop right where it is, but it keeps going past the error. Maybe 6 years later someone will answer?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Apr 2021 17:08:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-tool-function-error-handling/m-p/1045511#M60735</guid>
      <dc:creator>SarahSchwarzer1</dc:creator>
      <dc:date>2021-04-09T17:08:24Z</dc:date>
    </item>
  </channel>
</rss>

