By tornadoes and exceptions.
When writing a program and executing it, we often encounter errors and errors caused by not following
the proper structure or the syntax of the language is called the syntax error or the passing it.
So whenever we are writing one code and the code is not properly written in the respective syntax as
provided by Python, then obviously we are getting getting some syntax error.
What is the meaning of the term syntax?
Syntax means grammar.
So the grammar in which the grammar which you are supposed to follow to write the statements in Python
programming, if we don't follow that one, then syntax error meticulous.
So sometimes during runtime there are some other errors may take place and they can be also called as
a semantic errors.
So syntax error, semantic errors are there.
So why do it while writing about programs in any programming languages, these shut up errors may come
into the play.
So example, if we missed this column in the if statement, then the if imports are not proper, etc..
So example, if we miss Kolon in the if statement, then such error may take place, or if the imports
are not proper, then the errors meticulous.
No, these are the errors.
Now what are the exceptions.
So the exceptions are being raised whenever you are having any conditions.
So not let us discuss our Biton exceptions.
Here we're having some Biton building exhibitions, so just consider this exhibition and cause of error.
So one is the assassinator raised when Assad statement fails.
So we are having a separate video to discuss on this assertion.
Please watch that one.
And if that assertion is causing failure, then this assassinator will be raised next.
What is the attributed?
It raised one when the attitude, assessment or the reference fails now.
Next one is the if that is the end of dilator raised or when the input function needs to end up the
file and it is trying to read some content from the file.
So in that case, obviously the file has got exhausted and so the error will be, it will be is the
exception will be raised and that is known as E or if the next one is a floating point, either raised
when a floating point operation fails, generated exit, so raised when a director's closed method closed
method is called.
So there is a generated exit.
Next one is import error raised when the imported module is not found.
Let us what you are trying to import non-pay model, but the non-pay model is not present.
Then obviously the import error will come into the play.
So next, what is the index it raised when the index of a sequence is out of the range?
Let us suppose that having only five elements in the sequence that you want to be able to extract,
or do you want to pick up the gentleman from the sequence?
So obviously it will produce some index error.
So let us go for one practical demonstration, for the better understanding of this file of this python
errors and exceptions, indecision.
We are discussing Python errors and exceptions under the tree block we have written is equal to one
string here and we are trying to convert the string to the integer Anderson.
Value will be assigned onto the variable beat.
But here to see one exception would be amazing because we cannot convert high to an integer.
So let us execute this code and let us see that what will happen here to see this particular block has
got followed by the exit block.
So this exit block will be initiated whenever we are having some error, whenever we are having some
error within that block.
So what has happened is whenever you are trying to convert this E to the integer, it has produced one
error, and that is why the control has come directly to this exception block.
So here this exception block will be denoted by accept and Colon will be there if I write it one statement.
If I had one step, you can find that this statement is not getting executed.
That means from this particular line where that exception has been, the error has been raised automatically.
The control has come to the except block and living that is part of the crime block and executed and
unreachable.
But if I convert this one as 100, if I like this one is 100, you can find that I'm here.
It is getting printed.
That means that the full trade block has got executed.
But exception block, the exit block has got not executed here because that is no exception to convert
100, which is in the form of string to integer.
And as I need to be so we can print the value of behere.
You can find that to be easily 100 is getting printed.
So in this way, you have shown you that how this Trian except blocks are working here.
If I convert it to higher, once again, you're going to find that.
Except Block is getting control, so next one is this one, so getting specific exception, so here
is equal to five and being single digits digital and we are trying to do a baby so as single digits
here.
So a baby will produce one exception here.
This particular error we have given the name, there is a general division error.
So division by zero is not possible.
That is a message to be printed here.
So let's go for the execution.
You can find that division president is not possible.
So if you want to print the value of C.
You can find that that this particular line has become unreachable, but if I make this one out of five,
500 and if I make this one set, then in that case, obviously the except gyro division error, that
block will not get the control and it'll bring the record value for C as 50 here.
So that is our kaching specific exception.
So again, you are making this one at zero.
You can find that it is sprinting.
That division by zero is not possible.
And that means the exit block is getting the control whenever we are trying to divide it by zero here.
BS digital here.
So now let us go for another one.
So exception can be raised also in a in our call that we can raise some exception.
So let us suppose within the trade block we have raised one exception, one error that is a typo in
it.
And exit block is handling that type.
And this particular message is going to get printed.
So that means we can also raise our error within the trade block and extra block should have that particular
error code, and that's why the respective body will be executed.
So here you see we are having this type error instead of this type in it.
If we keep only this, except then also the code will produce this one address.
You can mention the you can mention except type error.
Then also the controller will be coming to this block and type error exception.
Got that message will get printed.
So now let us go for a try.
And finally, so within the try block with a written print in Try Block.
And here you see that is no exception has been raised.
So what did happen after the execution of the trade block?
Then the final block will be executed, except Block will not be executed because no effort has been
raised yet.
So what will happen?
The trade block will get executed and the final block will get executed.
So let us go for the execution.
So in trade block, in the final block, you can find this.
But if we raise one error, if it is one error using this code that is res type error, then what will
happen?
That trade block will be executed.
That means this particular code will be printed, this particular statement or string will get printed
and then this res typedef.
So content will come to this except and see what happens.
You can find that in block, in next block, in the final block that was finally blocked will be will
be executed in spite of the fact whether any error has occurred within the trade block or not.
So that means finally block is that the block which is supposed to get executed always, irrespective
of the fact whether any error has occurred within the trade block or not?
Let us suppose within the trade block we're opening one day to fight how to open one data file, how
to perform the read and write operations.
We have discussed that one in the previous videos.
So let us suppose we're opening one data file.
The data file will be open within the trade block and we are trying to read or write from the data file.
Let us suppose there is some error has occurred while reading the data file.
Some portion of the data file has fallen on the back sector of the hard disk in the case.
What will happen?
Some either it will take place.
So in that case, what will happen?
The exception will be raised and will come to the next block.
But in the final block we will be keeping all the F close.
That means the closing of the file and that really method will be called within the Holmdahl final block.
So what will happen whenever some error has occurred within the trade block, then the block will get
to control and ultimately the file will be closed.
But while performing the reading or writing operation from the data file, which is there, the code
has been written within the trade block.
If that is noise that has occurred, they not only also finally block will get the control and the file
will be closed accordingly.
So that means those particular codes are statements which are supposed to get executed irrespective
of the fact or that errors are occurring or not.
They must be written under the final block.
So in this way, in this particular demonstration, we're giving you the idea what is the purpose of
trade, block, block and a final block.
Thanks for watching this video.