In the previous video, you saw that if the user enters edit and then instead of entering a number,
they enter it to the item.
The program will end abruptly and that is not a good user experience.
So instead of showing this error to the user, instead of stopping the program, we want the program
to continue.
So what we want instead is if the user enters this, we will tell the user that.
They entered the wrong.
Command.
And then we will let the user enter the command again by showing them this prompt.
And that makes for a better user experience.
So never break the programs like that for the user.
So to not let the program and abruptly like this, we will use error handling.
So what is error handling?
Let me show you.
That's to you.
So here we are in the l if added case.
So if the command starts with edit we get.
The number from the commands.
The number is supposed to come after edit, but sometimes the user will enter something else.
So what we want to do is we want to first try out.
We want to try to execute these lines.
So everything under under the live condition, we want to try to execute it.
So what you do is you want to select these lines and then press tab so you indent them.
So now we have eight spaces and what do you do next?
You go here and you press enter and then you say try and I column.
So as you can see, I'm introducing a new code block, just like we had l f blocks, we had width blocks.
We also had the case blocks.
So everything under if under width is intended, the same goes for Troy.
So everything under Troy is indented.
You see all these lines, all of these are indented under trial.
So we try to execute these and if there is an error.
We go here in the same level with try to try is in the same vertical line as you can see and you say
except followed by the error.
The error in our case is value error.
So you go back here and you say value with a capital V error with a capital E
and a column.
So if there is an error, then we want to print out.
Your comment.
It's not valid.
And immediately after that, we want to give the user the opportunity to enter the command again.
So we want to show this message again.
And that message is produced by.
These two lines, so I'll copy them.
Go back here under the print function.
I want to paste them in here and I'll execute the code now.
So type and show edits complete or exit.
Let's say Ed did add a new member presenter and what we get this time is not an error.
It is this message Your command is not valid produced by that line.
And then we have this prompt again.
We can answer again the input.
So if you enter again the wrong command, add a second member or anything like that.
Presenter You get asked again for entering the input.
Now let's try to enter.
Edit one.
So when you enter it, it's one we get this printed out under Troy, you see print number printed out
number one.
And here we enter the new to do add a new player show.
And now as number one we have this adds a new player.
It's working, but it's not ideal.
The ideal solution would be not to have repetitive code because currently these two lines are the same
as these two.
That is not the best code optimization.
So what we can do actually is we use the opposite of a break statement instead of using these.
You remember the break statement breaks the loop and the continuous statement continues.
Or run another cycle of the while loop.
So what's going to happen now is this.
Let's say the user enters.
Edit Add a new member.
The program will try to run these lines.
And if there is an error, then these lines will be executed.
Actually, if there is a value error, because there are different kinds of errors out there, it is
important to specify the error here because you don't want to get confused with all kinds of errors.
So you want to execute these only when this error occurs.
So when that error occurs, you print out the message.
In the command line.
And then this statement here, what it will do is it will ignore everything that comes underneath here
and it will go back again to the beginning of the loop here in line two.
So it will execute this input function.
Let's try it.
Run the code.
Edit.
Add a new member.
Your command is not valid.
Type ads show edits, complete or exit edit Add a new member.
Again we get your command is not valid.
Type add show, edit or complete or exit edit to enter a new to do Update the OS press Enter show and
update The OS is number two now.
So that was an example of the tri accept block.
Before I close the video, I want to create another tri accept block here in this code because we have
another error here which might happen in the complete condition.
Because if we run the code and the user tries to complete.
A night in which is not in the list.
For example, item 23, when all we have is a few items and in that case we will get an index error.
It says that the list index is out of range.
So that's the same error as if we had a least.
For example, A and B, So with two items and you try to access item with index 23 or 24, so you will
get that error again, index error.
So let's quickly handle this error as well so that the user knows that he is out of the range of the
items.
Available in the list in the to do list.
So again, we want to select all of these.
So all the lines which are under live and we want to indent them by pressing tab and then we want to
add try here.
So we will try to execute all that code and then.
Catch an index error.
So that error there index.
Error.
So if that's the case, we want to print out.
There is no item with that number.
And then continue again.
So which means that if the user enters a number which is out of range, we show that message and then
we let the user enter.
A new commands again.
So continue will execute these lines again.
So now if the user enters complete 23, they get this message.
And they get the chance to enter command again.
So that was the lecture about error handling.
Let's keep going.