In the previous tutorial we have seen different exceptions.
Raised when executing our programs so when the Python interpreter finds a fault in our program that
means the program does not complete as expected or it generates erroneous results.
The python interpreted calls for that execution of the program and reported the error by throwing an
exception for some scripts.
This behavior is sufficient but it is nice to catch the exception and print a more user friendly version
of the error so we can decide how our programs respond to different types of errors or exceptions.
Say for example we may want to prompt the user for more input.
Display extended data or warning information exception handling mechanism allows us to deal with exceptions
and errors gracefully without halting the program.
And in Python we use the try.
Except statement for exception handling and this is the syntax for the try.
Except statement the Code begins with the word.
The key word try which is a reserved keyword in the key word is followed by colon.
The trade clause contains code that might raise an exception.
So these debtors set of statements here might raise an exception and they are put in the trade clause.
After that we have an except clause that starts with the reserved keyword except followed by the exception
type and a colon.
The exception type like we have discussed before can be the Zero Division error type error value error.
And so on and these exceptions are raised from the dry block and the accept blog contains go to handle
the exception when an exception occurs in the trade block execution of list of the statements in the
try block escape.
And if the exception dressed matches the exception type in the except clause the corresponding handler
is executed or the corresponding code is executed decode in the try block and raise any of the exceptions
like the Zero Division error type error value error and so on and the accept blog may declare what kind
of exceptions they handle.
That is the exception type and in the except blog we are going to write code or write a program that
is going to handle the selected exception.
So the response to the exception is put in the accept blog.
Say for example if the exception raised in the tribe blog doesn't match with the exception type specified
in the accept clause the program hard with the trace back No let us see examples of how the how exceptions
can be handled using the dry except statement so here we have declared two variables A and B and indeed
tribe blog we're going to add these two variables and then print digital and indeed except clause we
are going to catch the type error.
This is the exception type type error and this is of response to the exception type error.
If we're going to say that that the user needs to check the input variables the type of the input variables
when we execute this code since a and b the variables A and B are integers they can be added and then
the result is printed.
So there are no exceptions raised.
So the control does not go to the except clause so the statements here are skipped.
Now say for example this time we have assigned a string to the variable E and an integer to debatable
B.
We are trying to concatenate both of these variables and then this and then display the result.
And we also have the accept clause which is going to catch the type error.
Now when we try to concatenate these two variables since the data type of these two variables are not
the same one is a string.
The other is an integer.
This the try block code is going to raise a type error.
So on and then we have the accept clause which is going to catch this exception.
And this is going to be printed to the user check the type of input variables.
So instead of printing the end the entire error message giving details such as a file number line number
and the exception type detected we are going to print this text.
Another example where we are again trying to concatenate an integer and a string in this example we
have an except clause and this time we are catching the Zero Division error.
So when you execute this program did try.
Block is going to raise an exception which is going to be a type error because we are going.
We are trying to concatenate a string and an integer which is not possible there are different data
types so for that reason type error is raised but we have an accept clause which is handling Zero Division
error.
So your accept clause does not catch this exception and if the exception raised in the tribe blog doesn't
match with the exception type specified in the accept clause the program halves with a trace back.
So let's see this example in the Jupiter notebook.
So this is the example that we had just talked about it.
Execute this piece of code.
So as you can see the program has been terminated in the try block a type error has been raised and
since we are not handling or there is no accept clause that is handling this exception in our program
the program has been terminated and a full trace back has been printed in the output and accept clause
can name multiple exceptions in a tuple for example in this case multiple exceptions are included in
the accept clause separated by commas and enclosed in bed and this is a tri statement can have more
than one except clause so in this example we have an except clause to handle only type errors.
And here is the response to this type of errors and then we have another except clause in order to handle
name errors and then so on.
So each except clause handles different exceptions and at most one except clause will get executed.
Next we can also use the accept statement with no exceptions defined.
For example we have the accept clause here with no exception type defined so this kind of dry except
statement catches all the exceptions that are good.
In other words this is genetic for exceptions.
In this example we have the dry end except glass and we have more than one except glass.
In this example and each except clause handles a specific exception type so the input variables are
not debatable is a string the end.
The other variable is an integer and we are trying to concatenate these two variables and print the
result.
Let's execute this piece of code.
And here we have the response from the accept clause which is handling the type of and we have also
discovered that the last accept clause may Omer the exception names to serve as a wildcard so we can
provide a generic except class which handles any exception.
So the last except clause here is the generic except clause.
So when we do not specify any exception it will catch any exception.
So if we do not specify which exception to catch it will catch any but remembered on at most only one
except clause get executed in many programs simply reporting the error isn't enough.
If an error occurs we may have to undo some of the work that has already been completed.
Say for example the database may need to be rolled back or temporary files may need to be deleted or
the resources need to be released.
And in Python The finally clause is commonly used to define clean up actions which must be performed
under any circumstance so the finding Lee block is a place to put a neat code that must execute whether
the try block raised an exception or not.
So we have added the finally clause in our code.
Now let's execute this piece of code.
So there is a type error that has been erased from our code.
And then the control has gone to the except clause which is handling the type error exception from this
except clause.
We have this statement check data type of variables and after executing the accept clause the control
has gone to the finally clause and this statement is printed.
Say for example no exception has been raised from about code.
Let's change the data type of the variable.
Let's say this is is now assigned devalued trail and then execute the code.
So this time there is no exception.
Raise the the sum of these two variables has been printed in the output.
The control has gone to the finally clause and here the text has been printed and this brings us to
the end of the topic at and exceptions.