GIS Programming Module 3 – Debugging and Error Handling

Module 3 assignment dealt with debugging and removing errors in Python code. A Python error can be either a syntax error or a run-time error. Syntax errors are errors that are detected before the code is sent to the Python interpreter and prevent the code from running. Syntax errors will cause the code to abruptly stop. Syntax errors are generally caused by misspelling keywords, missing punctuation, or not indenting correctly. Run-time errors are logical errors in the code that do not prevent the code from running but produce incorrect results. Examples of run-time errors would be dividing by zero, undefined variables, trying to open a file that does not exist, and many others.  Run-time errors throw an exception that can be caught and handled using try-except statements.

Script 1 of the assignment prints a list of all the fields in the parks shape file. The script contained one run-time error and one syntax error. The run-time error was a NameError caused by a variable name that was not the same case as the defined variable. The variable FC was assigned to the name of the shape file but the lowercase “fc” was used as a parameter a function. The syntax error was a missing colon at the end of a for loop.

Flow chart for Script 1

 Output screen capture of Script 1 after errors were corrected.


Script 2 opens an ArcGIS project file and prints the spatial reference of the dataset and then prints a list of the layers the map contains. The script had many errors that mostly dealt with incorrect spelling or wrong case for variable or function names.

Flow Chart for Script 2


Output screen capture for Script 3 with errors corrected.



Script 3 was a practice of using try-except statements to catch errors in code but allow the code to continue and display error messages when complete. The script was divided into 2 parts where Part A contained an error and Part B had no errors. The error in Part a was due to a missing parameter in the ArcGISProject method. The try statement was placed after the print statement that displayed a message that Part A was running and before the statement that caused the exception. The except statement was placed after the message that displayed that the code had completed successfully. I did not want the successfully completed message to print if an error occurred. The except statement would print that there were errors in running Part A and print the error message. The code would then continue to Part B.

Flow chart for Script 3


Output screen capture Script 3


Comments

Popular posts from this blog

GIS Programming Module 1 – Python Environments & Flowcharts

GIS Programming Module 2 – Python Fundamentals

GIS 5935 Lab 1 - Calculating Metrics for Spatial Data Quality