This week our module covered debugging and error handling. This is perhaps one of the most important topics on the pathway to becoming a great programmer. There are a multitude of errors you will run into, so having the knowledge to fix them is astute. We were required to produce three debugging scripts with the last one including a try except statement.
In the first script, I had to identify two errors and fix them so the script would run. The successful script had to print out the names of the fields in the park.shp attribute table. Below is a copy of what my script looked like when it ran successfully.
In the second script, I had to identify 8 errors and fix them so the script would run. The successful script had to printout all the names of the layers in the project. The errors were mainly syntax errors and I was able to execute it in the same amount of time as I did the first script, around 15 minutes. Below is a copy of the script ran in the console pane.
The third and last script was the most complicated. It certainly did not take me 15 minutes like the other two. The script has two parts: A and B. Both parts must run in the final script. Part A should encounter an error and print an error statement stating the problem. Part B should run successfully, printing out the name, data source, and the spatial reference of each layer. The successful script should run both parts, returning an error message for Part A and printing three sets of messages for Part B. We were introduced to executing this by inserting a try except statement. I was able to identify the error in Part A very quickly by running the IPython debugger tool. It did take a while to locate where the try except statement should go and I did understand the correct syntax and indentation to occur. My biggest challenge was figuring out what the except statement was. I kept trying to make it more complex than it needed to be. Trust me, it is a simple except statement. Below is an example of the script ran in the console pane and a flowchart.
No comments:
Post a Comment