Thursday, June 11, 2015

GIS Programming- Debugging & Error Handling

This week in GIS5103- Programming, we worked on three Python scripts using Debugging and Error Handling techniques. The main focus was learning to use the Debugging tool in PyhthonWin. I found the "step" function to be extremely useful. Additionally, using the "step over" function prevents the internal libraries from popping-up when an error is detected.

step over button 
 This method for checking your script is great because the Debugging tool will indicate your error and display an error code that can be very useful in finding the solution to get your code to run from start to finish.  We examined three scripts this week. Script 1 was intended to print out the names of all fields on the Module4/Data/parks.shp attribute table, but there were two errors/exceptions I had to find first. Using the Debugging tools from above, I was able to correct the errors and successfully run the script:

The second script was supposed to print out the names of all layers in each data frame of the Module4/Data/TravisCountyAustinTx.mxd map, but it too had eight errors/exceptions. Using the tools above and going line-by-line, I found all eight errors and was able to run the script:

The third and final script had a Part A and Part B. Part A had an Exception we were not to fix. Instead we would use the try-except method.  The script was intended to  run Part A; encounter an error; and print an error statement; Part B would run successfully, printing out the Name, Spatial reference, and Scale of the data frame. 

There were many steps I used to complete script 3. Below are the steps I followed to apply the try-except statement:

1.  Initially, I ran my code normally, not using the Debugger tool, to see what error was present.
2.  After I detected which line of code had an error, I reviewed the try-except portion of the lecture video.  
o   I was concerned with the placement of the try- except block of code.
o   After reviewing both the video and the text book, I had a good idea of where to place my try-except code
3.  Initially, I placed the block of code before and after line 13 (mapdoc = arcpy.mapping.MapDocument() )
4.  My challenge was to determine the, beginning and ending points for the try-except block of code, so I used a line-by-line approach until I got my script to run correctly.

My lesson learned was to take note of the blocks of code as each step or function relates to its placement in the PythonWin window. I found it helpful to add empty lines between each action in the script as I used the step-over function. 

No comments:

Post a Comment