Module 4 - Debugging & Error-Handling

"Good programmers are those who know how to utilize debugging and error-handling most effectively" (Morgan). No matter how diligent we are in writing code, mistakes are bound to happen. In fact, in our lifetime, we will encounter 3 main types of errors in Python:
  1. Syntax Errors: which prevent codes from running; they pertain to spelling, punctuation, indentation, and capitalization mistakes;
  2. Exception Errors: (or things we didn't expect) refer to errors that are detected while the script is running and will stop running midway; and
  3. Logic Errors: errors that will run, but will produce undesired results.
[womp, womp, womp]

This week we explored various debugging and error-handling methods to examine and correct these types of errors and their error messages. Specifically, we learned how to recognize syntax errors and exceptions, use debugging by "stepping through code", error-handling by creating try-except statements, and practicing commenting-out code to keep track of our corrected errors.

For our lab assignment, we journey through three different, already-created scripts (with multiple errors), and debugged them as such:

In our first script, we corrected 2 syntax errors (capitalization and spelling), in order for the script to successfully print out the names of all field columns of a shapefile located in my student folder. A screenshot of my results are shown below.


In our second script, we corrected a total of 8 errors (syntax and exceptions combined), so that the script would successfully print out the names of all layers in each data frame from a specific mapping document (.mxd) located in my student folder. A screenshot of my results are shown below.


And lastly, for our third script, we added a try-except statement in Part A, to specifically "catch" and print the relevant error message (without actually fixing it); and have Part B successfully print out the name, spatial reference, and scale of a data frame from an .mxd in my student folder. A screenshot of my results are shown below.






Comments