- [Instructor] Now let's take a look
at how to handle actual exceptions as they occur at runtime,
so for this purpose,
we're going to be looking at a script
called dividebyzero.py,
let me go ahead and execute that
so you can see it in action.
This script is going to prompt the user
to enter two numbers,
a numerator and a denominator specifically for this script,
they must be integers,
so let's say I go ahead and enter 100,
now for the numerator, now for the denominator,
in the preceeding video,
we saw that if we divide by zero,
a zero division error occurs.
So, let's see what happens if we try to divide by zero
in this script.
We get an error message,
attempted to divide by zero
but the script doesn't terminate,
it simply prompts the user
to enter the numerator once again.
Now in the preceding video,
we also demonstrated what happened
if an integer was expected
and the user typed a non-integer value.
In that case, we got a valuer
but in this script, we simply get a message
you must enter two integers
and the prompt to enter the numerator appears once again.
So, finally if I do go and enter two integers,
then we can see the result
of dividing those two values
and the script terminates.
So, there's actually three flows of control going on
in this script.
There's the flow of control that occurs
when we attempt to divide by zero
and we display a message indicating that
and then simply continue by prompting the user
for input once again.
There is the flow of control
that occurs if the user types non-integer value
like hello, in which case again,
we get an error message and then we prompt the user
for the numerator once again
and then there's the flow of control
for a successful division
which does in fact lead us to terminating the script
after the division is performed.
So, let's take a look at the code for this
and those three flows of control.
So, for the code, we have this script
and we haven't looked at a script for a while,
so just as a reminder,
we always include a comment at the beginning
of the script file
that specifies the name of the file
so you can find it on disk
and we have a doc string that is used
to describe the purpose of the string.
Now in this script we have an infinite while loop
and the while loop is going to execute the statement
in its suite which is a try statement
in this example and as you can see,
the try statement has code in it,
in its suite and then it has a couple
of except clauses and an else clause,
so we'll talk about all the different pieces here.
Now, in Python, typically you're going to put
in the try suite any statements
that might cause an exception.
The else suite
is where you put code that you want to execute
if no exceptions occur
in the try suite, so in a lot of other programming languages
that have exception handling,
you put in the block of code
for the try, not only the statements
that might cause an exception,
but also any statements
that you don't want to execute if an exception occurs.
In Python that second category,
the statements that you don't want to execute
if an exception occurs,
those get placed in the else block
and they are in the else suite I should say
and those only execute if no exceptions occur
in the try block,
so back up here in the try statement,
we're going to prompt the user for some input,
the input function will obtain the value the user types,
give it back to our program as a string,
we will convert that string to an integer
and assign it to number one,
then we'll do the same thing again
for a second value that will represent the denominator.
If both of those successfully execute,
we will attempt the division.
If that successfully executes,
we will print the result of the division
by executing the else clause
which will also terminate the loop.
On the other hand, going back up above here,
if either of these first two statements in the try suite
causes a value error
because the user did not enter an integer value,
in that case the try suite terminates immediately
and control jumps down to the first
of the except handlers.
Now if I did not enter an integer,
the type of exception I'll get
from these first two statements in the try suite
is a value error,
so that would be the matching exception handler
for the particular type of exception that occurred.
In that case, we will print You must enter two integers.
Now once you reach the end of an except clause's suite,
that exception is considered to be handled.
All additional except clauses are skipped
as is the else clause, so once we print
you must enter two integers,
the rest of the try statement will be skipped
and control will continue with the first statement
after the try statement
but in this case, that's the end of our loop,
so we'll go back to the top of the loop
and we will try everything once again.
If we successfully read in the first two values,
the numerator and the denominator,
then we'll try to perform the division.
If the numerator is zero,
in that case, again the try statement
or the try suite terminates immediately.
If we had additional statements
before this first except clause,
they would not execute.
Control flow jumps to the except clauses
and this first except clause
is not for zero division errors,
so again we're talking about the case of dividing by zero,
in that case, we skip that except clause
and go to the next one.
That except clause does in fact match a ZeroDivisionError,
so this one will execute,
we will print Attempted to divide by zero.
If there were any further except clauses,
they would be skipped,
the else clause will be skipped
because we're in the exception handling flow
of control and we will proceed
with the first statement after the enter try statement
which in this case means go back to the top
of the loop and try it all again.
Now if we successfully read the two integers
and successfully perform the division,
all the except clauses will be skipped
and the else clause will execute.
That will print out the results of the division
and we will terminate the loop
at that point and because we have nothing more
in our script to execute,
the script will terminate in that case as well.