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:
<SPAN class="comment token"># Try to run code</SPAN>
<SPAN class="comment token"># May be ArcPy or another module</SPAN>
Try<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># some code</SPAN>
<SPAN class="keyword token">except</SPAN> Exception<SPAN class="punctuation token">,</SPAN> e<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># If an error occurred, write line number and error message to log</SPAN>
tb <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>exc_info<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
report<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed at step 1 \n"</SPAN> <SPAN class="string token">"Line %i"</SPAN> <SPAN class="operator token">%</SPAN> tb<SPAN class="punctuation token">.</SPAN>tb_lineno<SPAN class="punctuation token">)</SPAN>
report<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">.</SPAN>message<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Solution #2, which seems to work with non-ArcPy modules:
<SPAN class="comment token"># Try to run code</SPAN>
<SPAN class="comment token"># May be ArcPy or another module</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Some code</SPAN>
<SPAN class="comment token"># Catch errors and write error message</SPAN>
<SPAN class="keyword token">except</SPAN> Exception<SPAN class="punctuation token">,</SPAN> e<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># If an error occurred, write line number and error message to log</SPAN>
tb <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>exc_info<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
report<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Failed at Line %i \n"</SPAN> <SPAN class="operator token">%</SPAN> tb<SPAN class="punctuation token">.</SPAN>tb_lineno<SPAN class="punctuation token">)</SPAN>
report<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>