What is Best Way to Get an Error Message in Python?

5669
4
03-17-2017 08:36 AM
PatrickMcKinney1
Occasional Contributor III

I've been working on developing various Python scripts for the past year.  Typically these are run as scheduled tasks and write the results (successes and/or errors) to a text file.  I pulled my boilerplate code from the ArcGIS documentation.

Typically I will use e.message 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.  This has occurred using the FTP module and Shutil module.  It seems to occur with non-Arcpy errors.  After some research, I found alternate code for this (str(e)).  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.

Below is a sample of the two alternatives I've found.  I could also benefit from some understandable reading resources to help me with better handling errors.  It's a nice feature of Python, but I think I've only scratched the surface.

Solution #1, which doesn't seem to write error message for non-Arcpy modules:

# Try to run code
# May be ArcPy or another module
Try:

# some code

except Exception, e:
 # If an error occurred, write line number and error message to log
 tb = sys.exc_info()[2]
 report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)
 report.write(e.message)‍‍‍‍‍‍‍‍‍‍‍

Solution #2, which seems to work with non-ArcPy modules:

# Try to run code
# May be ArcPy or another module
try:

# Some code

# Catch errors and write error message
except Exception, e:
 # If an error occurred, write line number and error message to log
 tb = sys.exc_info()[2]
 report.write("Failed at Line %i \n" % tb.tb_lineno)
 report.write("Error: {}".format(str(e)))
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

Short of providing user-defined exceptions... if you have an idea what can go wrong, the python documentation is pretty well the best source.  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.  You have the added complication that you are running this as a scheduled event

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

By "FTP module" I assume you mean ftplib.  Both ftplib and shutil involve executing other system code outside of the Python environment.  For example with ftplib, there are parts of the networking stack involved to make connections to other machines.

I encourage you to read the Built-in Exceptions documentation if you haven't already.  Looking at that documentation, one can see that EnvironmentError is "the base class for exceptions that can occur outside the Python system.  For environment errors, 2 items are typically returned:  errno and strerror.

My guess is that the errors you are missing with your original code are environment errors.  I am guessing if you trap and handle environment errors separately, your code will work fine.  For example, something along the lines of:

try:
    # some code
except EnvironmentError, ee:
    print ee.strerror
except Exception, e:
    print e.message
BlakeTerhune
MVP Regular Contributor

I've had trouble with e.message on the rare case when there is nothing in the message property from whatever raised the error. Sorry, I forgot what it was that didn't have a message property.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Be aware that ...EnvironmentError has been merged with Os error as of python 3.3.

 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"

The post 2.7 documentation will provide the necessary details for the OS Exceptions should you need to work in future versions of ArcMap or current versions of ArcGIS PRO and don't want to implement User defined exceptions in either python 2.7 or 3.x