Module Three Debugging and Error Handling


As we move along into Module three, we sharpen our trouble shooting skills. This week we focused on debugging and handling errors. We were provided with three different scripts all requiring different tasks. Some of the learning outcomes for this week's module include the ability to fix syntax errors and exceptions. Being able to implement debugging procedures and modify scripts to run exceptions. Being able to interpret error messages and correctly use a try-except statement. 

The first script was a nice warm up, we were tasked with identifying and correcting two errors to previously written script. Once the script was corrected it was supposed to print out the fields of an attribute table as seen below. The easiest way for me to accomplish this was to run the script as is first and then address the error messages.

Script 1 results


 For the second script we were tasked with debugging a previously written script that contain several errors/exceptions. Once completion of correcting the errors the script should run smoothly printing the spatial reference for the project and the layers names in the project. I handled this task very similarly to the first task, by rerunning the lines of code and correcting the error messages until the script ran with no errors. 

Script 2 results



For script 3 we had a part A of the script and a part B of the script. For part A we were tasked with applying a try-except statement that allowed the script to still run even with the error without crashing, and it would print out the error that occurred during the running of the script. With adding the try-except statement it allowed part A and B to run all the way through. Then for part B we needed to fix any issue so that, that section of the script would run without any errors. I have created a flowchart below that shows the process of the new flow of the final script using the try-except statement. I started with importing arcpy module which allows Python to interact with ArcGIS. Then I used the print function to print "Running Part A...". Following that is where I added the try-except statement. From running the script as is first I was able to locate where the error was occurring, so I added the try-except statement above it. If no exception were to occur, the script would run through the entire lyrlist of the project, searching for a feature class named "parks". If a feature class is named "parks", the layer name is printed, the layer's visibility is turned on, and then the layer's labeling is also made visible. The project is saved, the variables in the script are deleted. Then if there is an error the project is saved, the variables in the script are deleted then the error statement is printed. Allowing both A and B to run completely. Finally printing a completed statement. Here below is the output of both A and B parts of the script. You can see that an error did occur, and the script printed out the description of the error and continued to run the rest of the script.

Script 3 results


Flowchart

Comments

Popular Posts