<?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: Catching errors that aren't a proper Exception in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634705#M49431</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is helping me narrow it down. I have &lt;SPAN style="font-family: 'courier new', courier;"&gt;ftp.quit()&lt;/SPAN&gt; in the finally block but it seems that trying to quit an ftp connection that has timed out throws the same error but as a different error object ("error_temp"). Additionally, error_temp has no attribute "strerror". I had &lt;SPAN style="font-family: 'courier new', courier;"&gt;ftp.quit()&lt;/SPAN&gt; in a try/except but I was only catching AttributeError or NameError.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the solution to this is two-fold:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Change error logging to use just &lt;SPAN style="font-family: 'courier new', courier;"&gt;err&lt;/SPAN&gt; insetad of &lt;SPAN style="font-family: 'courier new', courier;"&gt;err.message&lt;/SPAN&gt;.&lt;/LI&gt;&lt;LI&gt;Catch (and pass) all Exceptions on &lt;SPAN style="font-family: 'courier new', courier;"&gt;ftp.quit()&lt;/SPAN&gt; instead of just AttributeError or NameError.&lt;/LI&gt;&lt;/OL&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; ftp.quit()
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 02:59:52 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2021-12-12T02:59:52Z</dc:date>
    <item>
      <title>Catching errors that aren't a proper Exception</title>
      <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634699#M49425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;More specifically, I'm using ftplib and if the server times out, my traditional &lt;SPAN style="font-family: 'courier new', courier;"&gt;except Exception as err:&lt;/SPAN&gt; is not catching it. I found &lt;A href="http://stackoverflow.com/a/3169751"&gt;one solution&lt;/A&gt; to use &lt;SPAN style="font-family: 'courier new', courier;"&gt;except ftplib.all_errors&lt;/SPAN&gt; and it seems to work but I'd like to know if there's a more generic way to catch these "other" exceptions.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jun 2016 18:46:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634699#M49425</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2016-06-21T18:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: Catching errors that aren't a proper Exception</title>
      <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634700#M49426</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I gave up with try except blocks for returning error messages since I found that when something fails, I just follow the error stream back to its source.&amp;nbsp; Have you really found a difference in what is meaningfully returned with and without the blocks?&amp;nbsp; The only time that I use one is if I have to delete something and if it can't I produce a new filename and move on.&amp;nbsp; So I guess my comment is, what do you want to do if something might error out... quit? or do something else? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jun 2016 19:54:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634700#M49426</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-06-21T19:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Catching errors that aren't a proper Exception</title>
      <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634701#M49427</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to catch the exception because the script is running as a scheduled task that sends an email and logs a success or failure.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jun 2016 20:21:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634701#M49427</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2016-06-21T20:21:47Z</dc:date>
    </item>
    <item>
      <title>Re: Catching errors that aren't a proper Exception</title>
      <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634702#M49428</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What behavior are you seeing that leads you to say the errors aren't being caught?&amp;nbsp; What are you doing with the exceptions after catching them?&amp;nbsp; Logging?&amp;nbsp; Exiting?&amp;nbsp; Re-trying?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jun 2016 21:53:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634702#M49428</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-06-21T21:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: Catching errors that aren't a proper Exception</title>
      <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634703#M49429</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There's an FTP site hosted by another organization that we routinely download data from. One of the downloads is very large and takes a while to download. The FTP server has a 120 second timeout so if the download goes too slowly for whatever reason, the script is disconnected. The error message is from ftplib and says &lt;SPAN style="font-family: 'courier new', courier;"&gt;421 Timeout (120 seconds): closing control connection.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Our script has everything in a big try/except/finally. The main logic with a log success message is in the try, there's an &lt;SPAN style="font-family: 'courier new', courier;"&gt;except Exception as err:&lt;/SPAN&gt; block that logs a failure message, then there is send email in the finally. It only logs the success or failure, then sends an email with the result.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; main logic here
&amp;nbsp;&amp;nbsp;&amp;nbsp; log success message here
except Exception as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp; log failure message here
finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp; send email here&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I know it's skipping the except block because the failure message is not being written to the log but I get the email. I also wrote a little test script that just connects to the ftp, checks for a file, then sleeps for 150 seconds. Again, the &lt;SPAN style="font-family: 'courier new', courier;"&gt;except Exception&lt;/SPAN&gt; block does not catch the ftp timeout error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT&lt;/P&gt;&lt;P&gt;I just looked at the script again and I have it logging with &lt;SPAN style="font-family: 'courier new', courier;"&gt;err.message&lt;/SPAN&gt; instead of just &lt;SPAN style="font-family: 'courier new', courier;"&gt;err&lt;/SPAN&gt;. That might be it...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:59:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634703#M49429</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-12-12T02:59:50Z</dc:date>
    </item>
    <item>
      <title>Re: Catching errors that aren't a proper Exception</title>
      <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634704#M49430</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just tested out ftplib and forced a connection timeout.&amp;nbsp; The try-except block caught the error.&amp;nbsp; The err.message was empty, but err.strerror and err.errno were populated.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Jun 2016 16:57:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634704#M49430</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-06-22T16:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: Catching errors that aren't a proper Exception</title>
      <link>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634705#M49431</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is helping me narrow it down. I have &lt;SPAN style="font-family: 'courier new', courier;"&gt;ftp.quit()&lt;/SPAN&gt; in the finally block but it seems that trying to quit an ftp connection that has timed out throws the same error but as a different error object ("error_temp"). Additionally, error_temp has no attribute "strerror". I had &lt;SPAN style="font-family: 'courier new', courier;"&gt;ftp.quit()&lt;/SPAN&gt; in a try/except but I was only catching AttributeError or NameError.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the solution to this is two-fold:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Change error logging to use just &lt;SPAN style="font-family: 'courier new', courier;"&gt;err&lt;/SPAN&gt; insetad of &lt;SPAN style="font-family: 'courier new', courier;"&gt;err.message&lt;/SPAN&gt;.&lt;/LI&gt;&lt;LI&gt;Catch (and pass) all Exceptions on &lt;SPAN style="font-family: 'courier new', courier;"&gt;ftp.quit()&lt;/SPAN&gt; instead of just AttributeError or NameError.&lt;/LI&gt;&lt;/OL&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; ftp.quit()
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:59:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/catching-errors-that-aren-t-a-proper-exception/m-p/634705#M49431</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-12-12T02:59:52Z</dc:date>
    </item>
  </channel>
</rss>

