Module 2: Debugging & Error Handling

The second module introduced debugging and error handling. These skills will be very helpful as I continue in the class. In the lab, I modified scripts so they would run correctly or run with exceptions. Exceptions were caught by a try-except statement.

There are several notes I would like to record in working through the lab:

  • Check spelling and punctuation carefully
  • Use the r”….” file path format (I’ve found this to be the easiest. It allows me to copy the file path directly from file explorer.)
  • Double check all file paths

The first script had two undefined name errors. I capitalized an "F" and added an "s" to field. The code's print out is above. It is a list of field names in the parks shapefile.



The second script had several errors. I fixed file paths and the describe spatial reference. This code printed the spatial reference and the map's layers.



The third script was much harder to work with. I left the errors and had to add a try-except statement to catch any exceptions. The statement also printed a string and the error statement. This allowed the rest of the script to run. The second half of the script printed the name, data source, and spatial reference of the layer.

To make the try-except statement, I first ran the code to see where and what the error was. I then tried to place the try-except statement where I thought I would go. This did not work. Next, I commented out the code below the error to see if this would help. This method did help and I was able to selectively move my except line down the code to get it to run successfully. I wrote my except statement to print the string “There was an error.” and the error statement.

I had some issues with the indenting of the try-except statement. I went back to the original template code to see how the indenting was originally written. This helped me fix some indenting syntax issues I made when adding the statement. 

Comments