This week we covered rudimentary debugging and error handling in regard to encountering error messages when attempting to run code. The errors we engaged were syntax errors, which involve issues regarding spelling, indentation, capitalization, and other conventions within the python language, and run-time errors. Run-time errors are issues the code encounters while the script is running. This could be anything from incorrect methods and functions, incorrect data sources, or incorrect file paths. These errors are also known as exceptions.
We did not cover logical resolutions as those have to do with the logical ordering of the code which we are currently in the process of learning. All methods, functions, and statements used in the exercises were processes we have already covered. Learning to avoid errors of this manner is practiced as we are learning the Python conventions themselves.
The two sections of the lab were relatively simple. In both parts of the module, code was presented which held several syntax errors and exceptions. These were discovered by a simple attempt at running the code and being presented with an error message detailing the location in the script of the error and the object it was having issues with. I simply was able to scan the code and modify the incorrect uses of code conventions, syntax, and change the required exceptions. For example, the exception that which was presented in part 2 of this module contained an incorrect name for our ArcGis Pro session. Once this was highlighted by the error message, a simple exploration of the data folder provided the only session available which held a different name. A simple switch of the filepath rectified the situation.
Part 3 of the module challenged us to use a try except statement to isolate an error by having the code run except if it is encountering a specified error. The error message in our results declared that the code was encountering a type error. By placing the word try at the beginning of the code sequence and then except type error as e (defining this type error to a variable) beneath the final print function in the sequence, the code runs and will print the result and move on to the next part except if it encounters this error. If it does encounter the error, the program will declare it in the results page (I was using a Python shell) but allow the code to proceed to the next section of code.
Below is a rudimentary flow chart of the logic used to implement the try except statement:
Part 1: Part 2:Part 3:
Comments
Post a Comment