Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 5

699 bytes added, 01:22, 7 October 2019
INVESTIGATION 2: Exceptions and Error Handling
= INVESTIGATION 2: Exceptions and Error Handling =
:Running into errors in programming will be a common occurrence. You should expect that it will happen for any code that you write. In python, when an error occurs, the python runtime interpreter raises an '''exception'''. This section will teach show you how to catch intercept these exceptions complaints from the python interpreter when they happen and to allow direct the program python interpreter to continue running, or to stop program execution executing your code with a readable more user-friendly error message.
== PART 1 - Handling Errors ==
There is a <u>massive amount</u> of exceptionsthat can be raised by the python interpreter. Online references can be useful. If you are searching for a common exception check out the [https://docs.python.org/3/library/exceptions.html#exception-hierarchy Python Exception Documentation.]
In this section, we will provide examples of how to handle a few exceptions when creating Python scripts.
:#To start, open the ipython3 a python3 interactive shellsession. Before attempting to handle exception errors, let's create an error, and then see what the python interpreter will say and how to we can "handle" it:<br><source lang="python">
print('5' + 10)
</source>You should get an exception error similar to the following:<source>
</source><br>'''Question:''' According to the exception error message, what do you think caused the error?<br><br>
:#Click on the link '[https://docs.python.org/3/library/exceptions.html#concrete-exceptions https://docs.python.org/3/library/exceptions.html#concrete-exceptions.]' and scroll or search for '''TypeError'''. Take a few moments to determine what a ''TypeError'' exception error means.<br><br>You should have learned that the TypeError exception error indicates a mismatch of a type (i.e. string, int, float, list, etc). If Python doesn't know how to handle it, perhaps we could change the number into a string or change the string into a number or at least provide a more user-friendly error message.<br><br>If we don't want the user of our program to have to learn how to read Python exceptions (which is a very good idea), we can catch/trap/handle this error when it happens. This is done with a specific block of code called a [https://docs.python.org/3/tutorial/errors.html#handling-exceptions '''try clause'''] where you place code in-between the '''try:''' and the '''except:''' coding blocks. In a general sense, it works like a modified if-else statement, where the try statement acts as a test, and the except statement will or will not handle the exception depending if it occurs or does NOT occurthe python interpreter raises any exception when executing the code under the '''try:''' coding block. That is to say, If no error occurs in the code contained in between the '''try:''' and the '''except:''' sectionkeywords, the script python interpreter will continue as usual skip the code under the '''except:''' coding block, but if an error occurs in the except section'''try:''' coding block, then it can the python interpreter will be handled with additional directed to execute the code under the '''except:''' coding (block, which can instruct the python interpreter to perform more user-friendly processing, like an printing a more user-friendly error message).<br>Let's demonstrate how to handle our TypeError error with code that first does not contain an error and then similar code that DOES generate an error.<br><br>
:#The following code does NOT generate an error:<source lang="python">
try:
15
</source><br>You should notice that since there was NOT an NO runtime error, the Python script performed the required task.<br><br>:#The following code handles an exception error raised by the python interpreter and direct the python interpreter to provide a more user-friendly feedback that at least one of the values is not an integer:<source lang="python">
try:
print(5 + 'ten')
f = open('filethatdoesnotexist', 'r')
</source>
:#Study the error raiseed by the python interpreter carefully and closely. In particular, what is the name of the error (or exception).Now, catch and handle this exception error:<source lang="python">
try:
f = open('filethatdoesnotexist', 'r')
:#Use the following as a template:<source lang="python">
#!/usr/bin/env python3
# Author ID: [seneca_id]
def add(number1, number2):
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
ls CheckLab5.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab5.py
python3 ./CheckLab5.py -f -v lab5c
</source>
1,760
edits