Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 5

546 bytes added, 10:52, 5 September 2017
PART 1 - Handling Errors
TypeError: Can't convert 'int' object to str implicitly
</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 numberor at least provide a more user-friendly error message.<br><br>If we want to write this program safely, we can catch /trap/handle this error while it's happening. 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 occur. That is to say, If no error occurs in the code contained in the '''except''' section, the script will continue as usual but if an error occurs in the except section, then it can be handled with additional coding (like an user-friendly error message).<br><br>::#Let's demonstrate to handle our TypeError error by issuing code that first does not contain an error and then re-issue similar code that DOES generate an error.<br><br>::#Issue the following lines of codethat does NOT generate an error:<source lang="python">
try:
print(5 + 10)
15
</source><br>You should notice that since there was NOT an error, the Python script performed the required task.<br><br>
::#TypeError Exception:<source lang="python">
try:
print(5 + 'ten')
except TypeError:
print('not At least one of the values is NOT an integer')
not At least one of the values is NOT an integer
</source>
::#Try and Let's generate another type of error where we try to open a file that doesn't exist:<source lang="python">
f = open('filethatdoesnotexist', 'r')
</source>
::#To Now, to catch and handle this exception error specific error we could simply use, issue the following lines of code:<source lang="python">
try:
f = open('filethatdoesnotexist', 'r')
except FileNotFoundError:
print('no file found')
</source>::#<br>Multiple exceptions can also be caught at the same time, such as does not exist, is a directory, or we don't have permission. Try <br><br>::#To test out the error handling code (previously issued), try removing permissions from the file, or creating specify a directory instead of a regular file, and opening then try to open it. :<source lang="python">
try:
f = open('filethatdoesnotexist', 'r')
13,420
edits