<?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: What is the correct or best way to make a python / arcpy script bail out? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259479#M19960</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If this is a python script tool, it's best to take advantage of parameter validation on the inputs -- to filter the input choices and check inputs &lt;STRONG&gt;before the tool is even run.&lt;/STRONG&gt; This makes your tool far easier to use.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00150000002v000000"&gt;Help 10.2: Understanding Validation In Script Tools&lt;/A&gt;‌&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to drop out of your script for some other reason - without generating an error, you can create a custom exception and handle it (as suggested by others above):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14111533325036686" jivemacro_uid="_14111533325036686"&gt;
&lt;P&gt;class HappyError(Exception):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise HappyError("message")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # ...&lt;/P&gt;
&lt;P&gt;except HappyError as msg:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python" title="http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python"&gt;Proper way to declare custom exceptions in modern Python? - Stack Overflow&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 19 Sep 2014 18:45:10 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2014-09-19T18:45:10Z</dc:date>
    <item>
      <title>What is the correct or best way to make a python / arcpy script bail out?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259475#M19956</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You know, the script runs, check various inputs and so forth.&lt;/P&gt;&lt;P&gt;If a requirement is not met, eg the feature is not the correct type or something, then bail out and print a meaningful message.&lt;/P&gt;&lt;P&gt;Currently I use sys.exit(), but this is not ideal and throws an error in python.&lt;/P&gt;&lt;P&gt;So, how do the experts do it?&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;Neil&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Sep 2014 07:13:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259475#M19956</guid>
      <dc:creator>NeilAyres</dc:creator>
      <dc:date>2014-09-19T07:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: What is the correct or best way to make a python / arcpy script bail out?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259476#M19957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000000p000000"&gt;Understanding message types and severity&lt;/A&gt; section in the help has some good tips.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Depending on the severity of the issue you can use:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018v0000005w000000"&gt;arcpy.AddError&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000024000000"&gt;arcpy.AddWarning&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Sep 2014 08:45:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259476#M19957</guid>
      <dc:creator>OwenEarley</dc:creator>
      <dc:date>2014-09-19T08:45:02Z</dc:date>
    </item>
    <item>
      <title>Re: What is the correct or best way to make a python / arcpy script bail out?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259477#M19958</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I always used sys.exit()&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Sep 2014 15:04:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259477#M19958</guid>
      <dc:creator>BruceHarold</dc:creator>
      <dc:date>2014-09-19T15:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: What is the correct or best way to make a python / arcpy script bail out?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259478#M19959</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Read the the "&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//002z0000000q000000"&gt;Error Handling&lt;/A&gt;" page of the help docs.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Sep 2014 15:36:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259478#M19959</guid>
      <dc:creator>JeffWard</dc:creator>
      <dc:date>2014-09-19T15:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: What is the correct or best way to make a python / arcpy script bail out?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259479#M19960</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If this is a python script tool, it's best to take advantage of parameter validation on the inputs -- to filter the input choices and check inputs &lt;STRONG&gt;before the tool is even run.&lt;/STRONG&gt; This makes your tool far easier to use.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00150000002v000000"&gt;Help 10.2: Understanding Validation In Script Tools&lt;/A&gt;‌&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to drop out of your script for some other reason - without generating an error, you can create a custom exception and handle it (as suggested by others above):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14111533325036686" jivemacro_uid="_14111533325036686"&gt;
&lt;P&gt;class HappyError(Exception):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise HappyError("message")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # ...&lt;/P&gt;
&lt;P&gt;except HappyError as msg:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(msg)&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python" title="http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python"&gt;Proper way to declare custom exceptions in modern Python? - Stack Overflow&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Sep 2014 18:45:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-the-correct-or-best-way-to-make-a-python/m-p/259479#M19960</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-09-19T18:45:10Z</dc:date>
    </item>
  </channel>
</rss>

