Module 3: Debugging and Error Handling

The purpose of this lab was to implement debugging procedures, interpret script error messages, fix syntax and other errors, and modify scripts to run with exceptions. There were three scripts with errors that required debugging.

The first script was supposed to print out the names of all fields in a shapefile attribute table (parks.shp), but three errors had to be corrected before it would run correctly. All three errors were Syntax errors. Spyder marks some errors in the script code with an orange exclamation point before the line containing the error. In this script the errors were in Lines 12, 14, and 15. One error was a capitalization error and the other two were misspellings. After correcting these Syntax errors, the script ran successfully as shown below in the IPthyon console output: 

The second script was supposed to print out the names of all the layers in an ArcGIS Pro project, but several errors had to be corrected before it would run correctly. Because this script was a little longer and produced a different initial error, an OS error, I elected to debug the code using the Spyder Debugger which runs the script line by line, identifying errors. The OS error was on Line 13 and the Filepath name was incorrect, in that it identified the wrong folder and the project name was incorrect. I changed it to where the project file was saved on my S drive in the Data folder and corrected the project name. The next error was Line 14, which was a NameError where the word "filepath" was capitalized and should not have been. The next error was Line 15 where a backslash was incorporated in a filepath instead of a forward slash. Line 16 contained two Attribute errors, both from misspellings in the attribute module names (arcpy and spatialReference). The final error was on Line 21, which was also a misspelling. As an error was encountered in the Debugging session, the error was corrected in the script, saved and Run and the Debugging session was started again. With all errors corrected, the script ran successfully as shown below in the IPython console output:

The third script had two parts, A and B, and was supposed to produce an error message for part A and three messages for Part B containing name, data source and the spatial reference layer for each layer in an ArcGIS Pro project. I wanted to make sure there were no errors in Part B so I copied the Part B code into a blank, test script to test the code for errors. It ran and produced the expected results without error, so I focused on Part A. In order to figure out which line of the script in Part A had the error, I used the Debugger in Spyder to go through the code line by line until I hit the error. The error hit on Line 13 as TypeError: __init__() missing 1 required positional argument: 'aprx_path'. Because the error occurred at Line 13, this is where I began inserting the try-except statement with try: and inserting the except part below the print part of the script. I indented all the code below this line to account for the start of a new statement. Because I knew the type of error by using the Debugger the except part of the statement is written as except TypeError: with the next line as a print statement printing the error message “__init__() missing 1 required positional argument: 'aprx_path'.” With the try-except statement in place to "catch" the error, the script ran successfully as shown below from the IPython console:



Comments

Popular posts from this blog

Module 6: Proportional Symbol and Bivariate Choropleth Mapping

Module 2: Coordinate Systems

Module 4: Color Concepts & Choropleth Mapping