Hello all, the third week of GIS Programming (4102/5103) was to teach us how to debug code and handle errors within the code. The first main way to find errors in code is to check the module first to check for syntax errors. If a syntax error occurs, none of the code will even run because Python can not understand what was written. After finding potential syntax errors, runtime errors could be identified either by running the code to see the error message or by running a debug tool. I personally find it easier to run the code and see the error message but I understand that at times a debug tool would be very useful. We also learned of another way of handling errors to allow the script to run without necessarily fixing the problem. A try-except statement can capture an exception (or error) and allow the script to keep running without that part of the script. You can also use it to print the error that occurred, which can be helpful in fixing the error.
Part 1 of this assignment was a simple Python script that had two errors to be corrected. The output printed out the names of all the fields in a GIS dataset called parks.shp.
Part 2 of this assignment had more errors within it than Part 1 that needed to be corrected. The bulk of these errors were typos that would lead to the code not running. Although it seems simple to fix typos, they can sometimes be hard to notice. Important takeaways from this part included being carefully case-sensitive for tools and variables within Python and ArcGIS as well as making sure any data being referenced actually exists and/or is pathed correctly. The script was able to print out the spatial reference being used for the project and all the layers of the project.
Part 3 of this assignment was meant as an exercise to learn how to use try-except statements and print error messages while still running the code. The first step I took was to identify the error by running the code and then, using a try-except statement, capture the exception so that it would not stop the code. The first error was a TypeError so I used the except statement and assigned the error to a variable. The variable allowed me to print the original error message to display the problem in the output. I used another try-except statement for a NameError when saving the project. The same process was used for this one to display the error message. After Part A was dealt with, Part B was able to run without any problems and printed out the name, data source, and spatial reference for each layer.
A flowchart was created to help visualize the logic process of developing the try-except statements in the script.
Thanks for reading!
Joel Sexson
Comments
Post a Comment