<?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: How to raise exception from if statement in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242544#M18864</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Not quite sure what you are trying to do, but maybe a try: except: block will do what you want?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; servicequery(CreatedDate, now_minus_10)
except DataError as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Data error', err.message
except (NoDatainLast5, NoDatainLast10) as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'NoData', err.message
except Exception as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Something else went wrong', err.message
finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Clean up etc...'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 12:08:53 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2021-12-11T12:08:53Z</dc:date>
    <item>
      <title>How to raise exception from if statement in Python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242543#M18863</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is a general python question, I would like to write a function that essentially states if data is available do this, if not raise an error, and a finally block which does something else.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In another function I would like write another if statement that says if function 1 raised an error do this, if not then do this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My question is, how do I reference this raised user-defined exception from my first function?&amp;nbsp; Here is a code-block that I am working with.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, if there is a raised error for NoData, I would like to say in another function, &lt;STRONG&gt;if NoData is raised then do this....&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This block returns:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; File "C:/Users/Administrator/Desktop/DevSummitJSON_PySeminar.py", line 231, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; servicequery(CreatedDate, now_minus_10)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; File "C:/Users/Administrator/Desktop/DevSummitJSON_PySeminar.py", line 227, in servicequery&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise DataError, "Exception: Data is older than 10 minutes"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;__main__.DataError: Exception: Data is older than 10 minutes&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# severely edited, since improper indentation etc, precludes providing a test environment
import arcpy&amp;nbsp;&amp;nbsp;&amp;nbsp; # not in original
arr = #####&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # not in original
fc = ####&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # not in original
sr = ####&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # not in original
NumPyArray = arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitudeshape', 'latitudeshape'], sr)&amp;nbsp; 
i = 5&amp;nbsp; 
i2= 10&amp;nbsp; 
now_minus_5 = Start - datetime.timedelta(minutes =i)&amp;nbsp; 
now_minus_10 = Start - datetime.timedelta(minutes =i2)&amp;nbsp; 
class DataError(Exception):
&amp;nbsp; pass&amp;nbsp; 
class NoDatainLast5(Exception):&amp;nbsp; 
&amp;nbsp; message = '\nException: NumberTooBigError:\nYour number is too big. \nTry a smaller one!'&amp;nbsp; 
class NoDatainLast10(Exception):&amp;nbsp; 
&amp;nbsp; message = '\nException: NumberTooBigError:\nYour number is too big. \nTry a smaller one!'&amp;nbsp; 
def checkData(CreatedDate):&amp;nbsp; 
&amp;nbsp; if CreatedDate &amp;gt; now_minus_10:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise NoDatainLast10&amp;nbsp; 
&amp;nbsp; elif CreatedDate &amp;gt; now_minus_10:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise NoDatainLast5&amp;nbsp; 
&amp;nbsp; elif CreatedDate is NULL :&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise DataError&amp;nbsp; 
def servicequery(CreatedDate, now_minus_10):&amp;nbsp; 
&amp;nbsp; if CreatedDate &amp;gt; now_minus_10:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; NumPyArray&amp;nbsp; #see at the top
&amp;nbsp; elif CreatedDate &amp;lt; now_minus_10:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise DataError, "Exception: Data is older than 10 minutes"&amp;nbsp; 
&amp;nbsp; else:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "test"&amp;nbsp; 
servicequery(CreatedDate, now_minus_10) &lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: Dan Patterson
I had to reformat the whole script due either to an incorrect format selection or source code was wrong.  Added lines at the top of the code to facilitate testing using the edited script.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:08:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242543#M18863</guid>
      <dc:creator>GeoffreyWest</dc:creator>
      <dc:date>2021-12-11T12:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to raise exception from if statement in Python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242544#M18864</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Not quite sure what you are trying to do, but maybe a try: except: block will do what you want?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; servicequery(CreatedDate, now_minus_10)
except DataError as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Data error', err.message
except (NoDatainLast5, NoDatainLast10) as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'NoData', err.message
except Exception as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Something else went wrong', err.message
finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Clean up etc...'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:08:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242544#M18864</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T12:08:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to raise exception from if statement in Python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242545#M18865</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It seems like there is a problem with your indentation. Subsequent lines after the If and elif statements should be indented, shouldn't they?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 Apr 2015 07:02:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242545#M18865</guid>
      <dc:creator>SepheFox</dc:creator>
      <dc:date>2015-04-04T07:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to raise exception from if statement in Python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242546#M18866</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Geoffrey&lt;/P&gt;&lt;P&gt;I had to severely edit the code due to formatting errors.&amp;nbsp; Please correct any lines that I may have mis-corrected &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;Dan &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 Apr 2015 10:46:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242546#M18866</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-04-04T10:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to raise exception from if statement in Python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242547#M18867</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan, &lt;/P&gt;&lt;P&gt;I think I have been able to solve this with a return statement instead of raise.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 Apr 2015 12:59:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242547#M18867</guid>
      <dc:creator>GeoffreyWest1</dc:creator>
      <dc:date>2015-04-06T12:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to raise exception from if statement in Python</title>
      <link>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242548#M18868</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A single exception class can be used to trap all your errors and print your message. I think this is a lot less work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;class MsgError(Exception):
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass

&lt;SPAN class="keyword"&gt;try:&lt;/SPAN&gt;
&lt;SPAN class="keyword"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if CreatedDate &amp;gt; now_minus_10:&lt;/SPAN&gt;
&lt;SPAN class="keyword"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise MsgError("&lt;SPAN class="string"&gt;Your number is too big. \nTry a smaller one!")&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="keyword"&gt;&lt;SPAN class="string"&gt;except MsgError as msg:&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="keyword"&gt;&lt;SPAN class="string"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(msg)&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="keyword"&gt;&lt;SPAN class="string"&gt;except Exception as msg:&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="keyword"&gt;&lt;SPAN class="string"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Python error:\n" + msg)&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:08:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-raise-exception-from-if-statement-in-python/m-p/242548#M18868</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T12:08:56Z</dc:date>
    </item>
  </channel>
</rss>

