Changes

Jump to: navigation, search

OPS435 Python Lab 5

125 bytes added, 10:46, 5 September 2017
INVESTIGATION 2: Exceptions and Error Handling
::#To start, open the ipython3 shell:<source lang="python">
ipython3
</source>Before attempting to handle exception errors, let's create an error, and then see how to can "handle" the exception error.<br><br>::#Before we try and catch and detect exceptions, lets make sure we know a few ways Issue the following to make errors in python. The following code will create an exception error:<source lang="python">
print('5' + 10)
</source>
TypeError: Can't convert 'int' object to str implicitly
</source><br><br>Question: According to the error message, what do you think caused the error?<br><br>
::#Firstly, what to look for here is the exception name '[https://docs.python.org/3/library/exceptions.html#TypeError TypeError.]' The Type error means a mismatch of a type(string, int, float, list, etc). Python doesn't know how to handle it, should it change the number into a string or change the string into a number?
::#If we want to write this program safely, we can catch 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 inbetween the '''try:''' and ''''except:'''. If no error occurs in the code the except portion will be skipped.
13,420
edits

Navigation menu