Module 2 - Debugging and Error Handling

This second laboratory assignment of the course focused on using debugging techniques to correct pre-written codes so that they would run properly. I was able to use critical thinking skills and the debugging skills on Spyder to resolve the errors placed throughout the codes. 

For the first two parts of this assignment, I found a wealth of syntax and small grammatical errors in the code that resulted in it not being able to run properly. I utilized the debugger and other helpful features offered by Spyder. The debugger can be used to help identify errors by running through code line-by-line, ensuring no errors are found and that all variables are being tracked. The debugger works by manually setting “breakpoints”, or places in the code that can serve as stopping grounds to make sure the code prior to it runs smoothly. Then, the debugger will go through each set of code between breakpoints, allowing one to check for/fix errors and keep track of variables to ensure the code runs properly. 

I found that syntax can be checked in Spyder quickly by hovering over the yellow warning sign next to the code line numbers and seeing what the error message is. Spyder will also often underline where the error occurs in red (such as many word processing applications) to see where the syntax error is. One last quick check of syntax using Spyder would be actually running the code, where the error message will show what line the syntax error is, and will even display a small pointer (^) showing where in the line the error began. In Spyder, the error message will display what type of error it is, the line of code in which the error occurred, and a general message about the error. This is all useful information in isolating where the error occurred and determining how to properly correct it.

One major factor in the codes running properly was correcting the file path of the shape files, ArcGIS Pro file, etc. in the beginning of the codes. This was crucial in allowing the codes to run properly, as the data being used for the codes had to be located in the files correctly in order for the codes to work. This small, but significant change meant all the difference in the remainder of the codes.

------------------------------------------------------------------------------------------------------------------

                                                               Part 1 Script Flowchart/Output



------------------------------------------------------------------------------------------------------------------
Part 2 Script Flowchart/Output


------------------------------------------------------------------------------------------------------------------
Part 3 
Flowchart/Script Output



For the last set of code for this assignment, I wanted to pinpoint exactly where the error in the code was so that I could isolate it with the try-except statement and still get the code to run successfully. Even if part of the code contains errors, the try-except statement may be used to allow the script to run successfully. This is because the try statement lets you test a set of code for potential errors and the except code essentially handles the error accordingly. This is useful when one wants to check for errors in their code, or allow the rest of it to run properly by isolating a known error. Upon running the code, I was able to find where the error was by its line number. I then isolated this line by placing the “try” statement before the error line and indenting it appropriately. 

I found that the code would not run if I were to place the “except” statement right after the error line, because the other variables in future lines were not declared and would not run properly. Thus, I placed this part of the statement at the end of my code so that the rest of the code would compute properly and not generate unwanted errors. I found that I had to indent all of the lines inside of this statement in order for the syntax to be correct and for the code to run properly. Upon making these changes, I was able to get the code to run smoothly and then continued on with the final steps of this part of the laboratory assignment!