<?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 Best Way to Get an Error Message in Python? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370873#M29315</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've had trouble with &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;e.message&lt;/SPAN&gt; on the rare case when there is nothing in the message property from whatever raised the error. Sorry,&amp;nbsp;I forgot what it was that didn't have a &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;message&lt;/SPAN&gt; property.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 21 Mar 2017 16:50:51 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2017-03-21T16:50:51Z</dc:date>
    <item>
      <title>What is Best Way to Get an Error Message in Python?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370869#M29311</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've been working on developing various Python scripts for the past year. &amp;nbsp;Typically these are run as scheduled tasks and write the results (successes and/or errors) to a text file. &amp;nbsp;I pulled my boilerplate code from the ArcGIS documentation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Typically I will use &lt;EM&gt;&lt;STRONG&gt;e.message&lt;/STRONG&gt;&lt;/EM&gt; to write the error message. However, there have been a few times when my code in the Except statement writes the line number of the error but not the actual error message. &amp;nbsp;This has occurred using the FTP module and Shutil module. &amp;nbsp;It seems to occur with non-Arcpy errors. &amp;nbsp;After some research, I found alternate code for this (&lt;EM&gt;&lt;STRONG&gt;str(e)&lt;/STRONG&gt;&lt;/EM&gt;). &amp;nbsp;I'm trying to figure out the best way to capture an error message (whether from ArcPy or another module) and write it to the log file.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Below is a sample of the two alternatives I've found. &amp;nbsp;I could also benefit from some understandable reading resources to help me with better handling errors. &amp;nbsp;It's a nice feature of Python, but I think I've only scratched the surface.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Solution #1, which doesn't seem to write error message for non-Arcpy modules:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;# Try to run code&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# May be ArcPy or another module&lt;/SPAN&gt;
Try&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# some code&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;except&lt;/SPAN&gt; Exception&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; e&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
 &lt;SPAN class="comment token"&gt;# If an error occurred, write line number and error message to log&lt;/SPAN&gt;
 tb &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; sys&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;exc_info&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;2&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
 report&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;write&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Failed at step 1 \n"&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Line %i"&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;%&lt;/SPAN&gt; tb&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;tb_lineno&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
 report&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;write&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;e&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;message&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Solution #2, which seems to work with non-ArcPy modules:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;# Try to run code&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# May be ArcPy or another module&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;try&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Some code&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Catch errors and write error message&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;except&lt;/SPAN&gt; Exception&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; e&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
 &lt;SPAN class="comment token"&gt;# If an error occurred, write line number and error message to log&lt;/SPAN&gt;
 tb &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; sys&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;exc_info&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;2&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
 report&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;write&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Failed at Line %i \n"&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;%&lt;/SPAN&gt; tb&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;tb_lineno&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
 report&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;write&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Error: {}"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;e&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:11:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370869#M29311</guid>
      <dc:creator>PatrickMcKinney1</dc:creator>
      <dc:date>2021-12-11T17:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: What is Best Way to Get an Error Message in Python?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370870#M29312</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Short of providing user-defined exceptions... if you have an idea what can go wrong, the&lt;A href="https://docs.python.org/3.6/tutorial/errors.html"&gt; python documentation&lt;/A&gt; is pretty well the best source.&amp;nbsp; I personally would like to know the state of progress until the line of failure, hence, I don't like or use try, except blocks using print statements or logging while in process.&amp;nbsp; You have the added complication that you are running this as a scheduled event&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Mar 2017 23:37:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370870#M29312</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-03-17T23:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: What is Best Way to Get an Error Message in Python?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370871#M29313</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;By "FTP module" I assume you mean &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;ftplib&lt;/SPAN&gt;.&amp;nbsp; Both &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;ftplib&lt;/SPAN&gt; and &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;shutil&lt;/SPAN&gt; involve executing other system code outside of the Python environment.&amp;nbsp; For example with &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;ftplib&lt;/SPAN&gt;, there are parts of the networking stack involved to make connections to other machines.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I encourage you to read the &lt;A href="https://docs.python.org/2/library/exceptions.html" rel="nofollow noopener noreferrer" target="_blank"&gt;Built-in Exceptions&lt;/A&gt; documentation if you haven't already.&amp;nbsp; Looking at that documentation, one can see that &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;EnvironmentError&lt;/SPAN&gt; is "the base class for exceptions that can occur outside the Python system.&amp;nbsp; For environment errors, 2 items are typically returned:&amp;nbsp; &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;errno&lt;/SPAN&gt; and &lt;SPAN style="font-family: courier new,courier,monospace;"&gt;strerror&lt;/SPAN&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My guess is that the errors you are missing with your original code are environment errors.&amp;nbsp; I am guessing if you trap and handle environment errors separately, your code will work fine.&amp;nbsp; For example, something along the lines of:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;try&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="comment token"&gt;# some code&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;except&lt;/SPAN&gt; EnvironmentError&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; ee&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; ee&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;strerror
&lt;SPAN class="keyword token"&gt;except&lt;/SPAN&gt; Exception&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; e&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; e&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;message&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:11:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370871#M29313</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T17:11:17Z</dc:date>
    </item>
    <item>
      <title>Re: What is Best Way to Get an Error Message in Python?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370872#M29314</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Be aware that ...EnvironmentError has been merged with Os error as of python 3.3.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&amp;nbsp;Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass"&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The &lt;A href="https://docs.python.org/3.6/library/exceptions.html"&gt;post 2.7 documentation&lt;/A&gt; will provide the necessary details for the &lt;A href="https://docs.python.org/3.6/library/exceptions.html#os-exceptions"&gt;OS Exceptions &lt;/A&gt;should you need to work in future versions of ArcMap or current versions of ArcGIS PRO and&amp;nbsp;don't want to&amp;nbsp;implement &lt;A href="https://docs.python.org/3.6/tutorial/errors.html#tut-userexceptions"&gt;User defined exceptions&lt;/A&gt; in either python 2.7 or 3.x&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 18 Mar 2017 08:16:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370872#M29314</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-03-18T08:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: What is Best Way to Get an Error Message in Python?</title>
      <link>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370873#M29315</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've had trouble with &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;e.message&lt;/SPAN&gt; on the rare case when there is nothing in the message property from whatever raised the error. Sorry,&amp;nbsp;I forgot what it was that didn't have a &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;message&lt;/SPAN&gt; property.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Mar 2017 16:50:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/what-is-best-way-to-get-an-error-message-in-python/m-p/370873#M29315</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2017-03-21T16:50:51Z</dc:date>
    </item>
  </channel>
</rss>

