Now we know when we run our program, the program will continue executing until we press control, see,
because we're literally trapping the program in this wild loop right here.
And when we press control C, the program will quit.
But it will also display this ugly error message.
Now, this message is actually very helpful when something is going wrong, because sometimes when you're
doing when you're programming, your program might crash in the middle of execution.
So you'll actually get a message similar to this one telling you what went wrong so that you can fix
it.
Now, in our case, nothing went wrong and the program crashed because the user entered control.
C And you can also see from this message it's telling you that the reason why all of this happened or
the reason why the program quit execution is because of a keyboard interrupt.
So this is a type of an exception that can happen during the execution of the program.
Now, like I said, there are many types of exceptions, and sometimes you're going to need to be able
to handle this exception in a certain way so that your program continues executing or so that it can
adapt to the exception that happened.
Now, in this lecture, I'm going to show you a very simple example of handling this exception.
So whenever the user enters control C, we're going to be able to capture this exception and do something.
In today's example, we're only going to print a statement and hide this ugly error message.
But the same method can be used to execute any type of code when the exception happens.
And I'm actually going to show you an example of that in the next lectures where we'll be restoring
the ARP values to the way they were.
But first, let's learn how we can handle exceptions in Python.
The way to do that, first of all, you want to see where your exception might happen.
So we know that our exception is happening in this wild loop right here.
So what we're going to do is we're going to say, I want you to try to go and execute this wild loop.
Now, the try statement is similar to the wild and to the if statements so that it takes a block of
code to execute.
So the block of code that we want to execute in this try statement is this wild loop.
So basically we're telling Python that I want you to try to execute this code right here, but at the
same time, I want you to accept.
A keyboard interrupt and I basically got this keyboard interrupt or the name of the exception from the
error.
So you can see that it's given us the name in here, which is keyboard interrupt.
So what we're saying is we're saying that python, please, I want you to try to execute this while
loop and I want you to expect or accept this kind of error.
If this error happens, I'm going to give you a block of code to execute as well.
And in here, you can use a function.
You can use any type of code that you want.
But in this very simple example, we're just going to print.
And we're going to say detected control Z quitting.
And that's it.
So let's have one very quick look on the code.
We're using a try and accept statement.
The try statement takes a block of code to execute.
So this is what's going to happen when Python interpreter reaches this part.
It will never execute the exception part unless this exception happens.
So the loop is going to keep running.
Or the block of code that you use in the try statement will keep running until this error happens.
So like I said, this error can be any type of error.
For example, in the previous lecture, when we use the integer in the print statement here, you can
use a try to detect that.
In our example, we're waiting for a keyboard interrupt because that's what's what happened in here.
So this code in the try block will keep executing until Python detects a keyboard interrupt or someone
presses control.
See if someone presses control.
See, we're going to print this statement right here.
So let's run our code again.
And as you can see now, the code is running normally.
Now I'm going to press control.
See?
And as you can see, we got a message saying detected controlled C quitting.
So we didn't get this error message in here.
Now this is actually very handy and you'll see the power of it as we write more complex programs and
as you start learning more about Python.
Right now, we're using it in a very simple way because I want to give you an introduction of it, but
you'll see in the next lectures we're actually going to be implementing more code in here so that when
the person presses control C, we will actually fix the network and restore the ARP tables to the way
that they should be set or to the way that they were before running our attack.