Module 3 - Debug and Error Handling

This week focused on Debugging and Error Handling. Three scripts were provided, in which each script contains multiple errors that prevented the script from creating the intended output. Script #1 included a syntax error, in that a word was capitalized incorrectly. After correcting the error, the script could run using ArcGIS Pro Notebook and print each of the names of all fields in a provided parks polygon shapefile. A screenshot of the output is shown below: 


Script #2 had several errors that needed to be corrected, such as misspelled commands, capitalized letters that should not be capitalized, and using / instead of \ in a text string. The final script needed to print out a comment on the project's spatial reference and list the layers in the map. By running the script and working through and correcting each error message, one line at a time, this produced the final result below: 


Script # 3 also included errors. However, the assignment was to use a try-except statement to capture the error without correcting it. An example of a try-except statement is below: 


By running the script, the error code noted the line that contained the error. In order to capture the error, the try statement was placed in the line above this line. To catch any exception, even an unknown exception, a variable  was assigned to the generalized Exception, and then a print statement was included to print the error. At first, I placed the except statement in the next line below the error. However, this created an additional error, which stated the variable is not defined. This is because the try statement contained the line that defines the variable that is needed in later lines of the script. Therefore, the except statement needed to be placed after all statements that used the same variable. In this case, the except statement needed to be placed before the final print statement. The final script runs, despite the error, by using the try-except statement correctly. The image below shows the error message generated but the script continues on to part b to name the layer, data source, and spatial reference for each layer. 



Comments