Debugging and Error Handling techniques were covered in this Module. Syntax errors, exceptions, and logic errors can prevent your script from running or produce undesired results. Common syntax errors are spelling, punctuation, and inconsistent or incorrect indentation. Often these types of errors can be caught by eye, but the Check button in PythonWin is an easy way to locate and find syntax errors. Our first task was to edit Script 1 and correct errors that were preventing it from running and printing the field names in a specified shapefile. Python is case sensitive so variable names have to be consistent. The error free script will produce the following results:
Results from successful run of Script 1 |
The following is a flowchart for Script 1:
Flowchart for Script 1 |
Script 2 our second debugging task, was more difficult. Because of my limited knowledge of Python I found Chapter 10 of our Zandbergen text to be very helpful in explaining the context of the mapping functions called in the provided script. There were 8 errors in this script and I used different techniques to help me locate and correct them. Reading the script and using the Check button helped identify syntax errors. I also used the debugger tool in PythonWin to step through the program line by line. I became more comfortable using this tool and understanding the step over and step out tools. I also used a try-except statement, so I would understand how to 'handle' exceptions. I chose to comment out the try-except as it was not needed to successfully run the script. This script, once error free, printed the layer names for layers found in a data frame of a specified shapefile.
Results after successfully running Script 2 |
Script 3 had two parts. There was an error in Part A of the script that we were asked not to correct. I found the error, but used a try-except statement to 'handle' it, instead of correcting it. The try-except statement enabled the script to successfully run through the second part of the script, Part B. The 'except' statement "handled' the error and printed the error message for Part A. When using a try-except statement it is imperative that you indent the entire block that is being 'trapped'. Part B printed the data frame name, the spatial reference, and the map scale.
Results of Script 3 after applying a try-except statement to trap and handle the error in Part A |
While I initially felt I wasn't prepared to handle this Module due to my minimal knowledge of the Python language, I now understand the importance of having this module relatively early in the semester. Debugging and error handling will be necessary in all future scripting endeavors and a basic understanding of the various techniques I am sure will prove invaluable in future assignments.
No comments:
Post a Comment