Sunday, June 2, 2024

Python Debugging and Error Handling

This week, we covered debugging and error handling in codes. We had three different codes that we had to find and fix errors in.

For the first code, we had to fix two errors. I was able to spot the capitalization error before even trying to run this code. When I ran it at first, I realized that I had my data in an additional subfolder so I got an additional third error in the code. I moved the files into the main Data folder and things went better from there.


The second code was a little bit tricker as I didn’t know how many errors I might get. I was able to fix the bad file paths quickly but it took me a little bit longer to sort out m. I knew it was a variable that hadn’t been described but the solution. The for-in statements are interesting because I’m never sure exactly what Python just knows, or what its pulling from something from inside a file like a field name or map name. I will read up more on these!


In the third script, we weren't supposed to fix the error but isolate it with a try except statement so that the other portion of the code would work. This part gave me quite a time trying to figure out how much of the code to bypass. I definitely just kept trying it in different places. In the end, I realized the answer was there all along in the error message: Line 13. It was far more of part a than I expected! The indents also got tricky to sort out how many each line needed.

Here's what the flowchart of the code with the statement. Part A of the code tries to run the code in the script to print the layer names. However, if it gets an error, it skips this code and prints the error message instead.



No comments:

Post a Comment

GIS Portfolio

To show off all I have learned during my GIS Graduate Certificate program, I created an online portfolio. Click here to check it out.  The ...