Monday, June 2, 2025

Module 3: Debugging and Error Handling

 Module 3: Debugging and Error Handling

In this weeks module the focus was on how to debug code and understand what the error messages are saying to the person writing or using scripts. There are different errors that can be seen when writing script. The first common error message is syntax error as known as the "grammar" of the code. This includes the spelling, punctuation, and indentation. On the other hand there are run-time errors that come up after the code gets run. Some examples are NameError, TypeError, and LicenseError. When doing this week we did three different scripts that we either fixed or did a try-except statment.

In the first script we had to look for two errors that were within the code. When trying to figure out the errors you can either click the "Run Module" or "Check Module" within IDLE. For me the easiest one was to save the script and clicking "Run Module". Then it comes out saying what the error is and what line it is located on. The first error was a NameError that within Line 5 the name 'fc' is not defined. When looking at the rest of the code it showed that the name 'fc' needed to be capitalized. The second error was another NameError in Line 8 where the name 'field' was not defined. Once again looking at the rest of the script I was able to notice I needed to add an 's' to the end to match other portions of the script. The picture below is the output of the script that was written.


The second script there were several errors or exceptions. Once again it was easy to save the script and click "Run Module" to find the first error. The first error was a NameError in Line 6 that indicated 'Filepath' is not defined. When looking in the rest of the code you can see it needed to be 'filepath' in the parenthesis. Next was an OSError which indicates that the file path in Line 6 is not connected to anything on the computer. I fixed the OSError to filepath ="S:/GISProgramming/Module3/Data/ Mod3_Data/TravisCountyAustinTX.aprx". The next error was a NameError in Line 8 saying 'arcp' is not defined and I noticed it was the dataset 'arcpy' that needed to fixed. Afterwards there was another OSError found in Line 7 and noticed one of the slashes were in the wrong way and that I needed to add more to the dataset file path. Then the next error was AttributeError that says 'spatialRefernce" does not exist. All I had to do was spell it correctly as "spatialReference." The next error was a TypeError that says 'type' object is not iterable in Line 13 and all I needed to do is add a 's' to the end of map. After fixing all the errors and exceptions it finally produced the outcome of the spatial reference and the four shapefiles within the ArcGIS Pro map which is seen in the picture below.


The third and final script that was written had errors but we needed to do a try-except statement. This statement allows a script to run successfully which helps with seeing the exceptions within the code and “trap” it so the code can continue run. When including the try-except statement you want to run the code to see what the error is that is stopping the code from running. First you want to put “try” statement before the code that has the error(s). Next you want to then put the “except” statement below the error and handles the error. Then you will run the code again to see if there are any other errors in the code. If there is another error, you can include in the current “except” statement or make another. Lastly, when you have all the errors it should be able to run the code and explain to someone why there were some errors as well as where they were located. I have included the output of both the parts and a flow chart of how to do Part A of the try-except statement.


In this module it was very helpful to learn about the different errors so that in the future my script runs without issues. Also, if I do run into issues I can fix it easily by being able to read what the errors and fixing them. Lastly, learning how to do the try-except statement lets the code run even if there are some errors that you might not necessarily know how to fix at the moment.

No comments:

Post a Comment

Blog Post #5: GIS Portfolio

 Blog Post #5: GIS Portfolio In the final weeks for the GIS Internship we were given the task of creating a GIS portfolio either on paper or...