You already know that when you try to do something that doesn't make sense, for example, trying to
add 12.3 to the string high, you get an error.
So that is the error.
But what you probably don't know yet is that there are two major types of errors in Python.
You have syntax errors and then you have exceptions.
So we have been referring to these as just errors.
But in this code experiment, video today will explain you the distinction so that you can distinguish
what syntax errors are and what exceptions are.
So for that, we're going to be working on our app.
And here is what I would like you to try for.
Now.
So please go under live where the complete condition is.
So where we check if the user command starts with complete and then we have this try block where we
try to get the number after the complete command.
So if the user enters complete three, then we get the part of the index nine, which should give us
the number and then we have the file operation here.
Then we remove the to do with that number from the list and we write it to do in the list.
Now what happens if we remove these brackets here?
So try to think what will happen.
We do have this try and accept block Right.
So that means the interpreter should try to execute this block.
If it finds an error in that block of code, then it should execute these lines under except that's
how try and accept works.
But is that true?
So are we going to get this printed out?
So let's try run and this is what we get.
Syntax error closing in parentheses does not match opening parentheses.
So why didn't the try accept block handle the error?
Why do we get a python error here when we were expecting this to be executed?
Well, the reason is that this here is a syntax error and the try accept block only catches exceptions.
What is the difference between a syntax error and an exception?
Well, let's suppose that you are sending an email to someone and on your browser you have a grammar
checker.
So that's Checker is checking for grammar errors.
Maybe you forgot a period or you typed in a word incorrectly so that grammar checker will check for
syntax errors.
So when the grammar checker tells you that oh, everything is okay, then you send all the email to
a person.
But then the fact that the syntax is correct doesn't mean your email content doesn't have issues.
So the grammar checker is only able to check for syntax errors, but then the person on the other side
they can check for logical errors.
So maybe you said that oh, there is a file in the attachment, but you forgot to attach a file in that
email.
So that would be some sort of logical errors which the grammar checker cannot check.
Similarly with the Python interpreter, there are two instances of that interpreter program.
So first the interpreter checks your code for syntax errors.
It doesn't go into the logic of the code itself.
It doesn't check conditions, it doesn't check try accept blocks.
So it just looks at the syntax model.
IDs also have utilities integrated, which also are able to check for syntax errors.
So all these other lines that you see are suggestions by the OR IDE.
And then when you run the codes you get the actual source of the error, which in our case points to
line 46.
So that's one in there where we have a missing square bracket.
So that is a syntax error.
Now sometimes you'll also see that syntax errors are referred by people as just errors, and logical
errors are referred to as exceptions.
So a logical error, for example, would be something like if we try to remove an item using an index
which does not exist in the list.
So for example, if the list has five items but we try to remove eight here, so the item with index
eight, then we would get an exception.
So in other words, if I open the python console in here, so if we have a list and we have A and B
as items than a dot pop and you say remove item with index five, that is not a syntax error.
So this is an exception, although you will see people, almost anyone will just say, Oh, we got an
error.
So they refer to errors in general to syntax errors and exceptions as well.
So from a practical point of view, the difference between syntax errors and exceptions doesn't matter
as much as it matters from the theoretical point of view.
So you should be aware that these differences exist this way you have a better understanding of how
your programs work.
It is beneficial in certain cases, such as in here.
When you use this try except block.
Now you understand that a syntax error is not code from this block because of the way the interpreter
works.
So it has this grammar checker component which checks the syntax first, and then it has the actual
interpreter which interprets what you were trying to say through the correct syntax.
Once the syntax is correct, the grammar checker gives the green light, then the interpreter component,
which understands the language, goes on and translates the instructions into machine language.
Then your processor processes the information and it gives the output on your computer screen.
And so that was the experiment I wanted to try for today.
And I'll talk to you in the next videos.
See you.