 Hello everyone, my name is Nikhil Gajam, working as assistant professor in department of computer science and engineering in Balchand Institute of Technology, Sallapur. In today's session, we will see exception handling in Python. Learning outcome, at the end of this session, student will be able to write a Python script for handling an exception. What is exception? Generally, there are two types of errors are there, one we call it as normal errors and second one is exceptions. Error is generally which are occurring during compile time and then exception which are occurring during execution time. So what is exception? Exceptions are special events which are handling or occurring during execution of your program, which are because of the user input generally or may be program errors are also occurring which causes an exception generally. So an exception is an event which occurs during execution of your program that disrupts the normal flow of program's instructions. You can defend your program by placing a suspicious code inside a try block and then after try block include an accept statement. So how to handle this exception? In Java you have try cache block, in Python you have try and accept block. So if you think that this is a suspicious code or exception may be occur during the execution of your program, all the statements generally you are including inside a try block. If exception occurs then control will be jumped to the accept block and execution will be or the statements which are present inside accept block will be executed. So all the suspicious code which you think that exception may occur during execution of your program, all the statements generally belonging to try block and if exception occurs then accept block will be executed. If exception does not occur in the try block then accept block will not be executed. So this is how you are handling the exception in Python. Now there are some examples of exceptions as divide by zero exception. Suppose you have written a code for division of two numbers and suppose second number is zero. So obviously division is not possible so this is an exception. So this is not the fault of actually a programmer. Programmer has written the correct code but the value which is entered by the user a second value is zero. So division is not possible so this is an exception. Suppose another exception may be a file not found. Suppose you have written a code for reading the file or writing content into the file. And suppose during execution suppose that file is not present at the particular location. So this is an exception occurred during execution of your program. So these are the some examples of exceptions in Python. So how to handle that? So you have a try and accept block. In Java you have try and catch block, in Python you have try and accept block. So this is the syntax for try and accept block. So there is generally a single try block is present and multiple accept block may be there for a particular try block. So try then colon, then all the statements belonging to try those are inside this try block. And generally you include all the statements you think that exception may be occur those statements are included in the try block. Then accept exception 1, then block of this code of exception 1, then accept exception 2, then block of the code for this exception 2. So this multiple accept block may be present in your Python file. Then other code may be also present. So common exception these are the common exceptions. First one is zero division errors. This happens when you have written the code for division of two numbers and second number is zero and occurs when a number is divided by zero. Obviously is divided by zero it is not possible. A second one is name error. It occurs when name is not found it may be a local or global. Third one is indentation error. If incorrect indentation is given then exception is indentation error. Then IO error it occurs when input output operation fails and end of file error. It occurs when end of file is reached and yet operations are being performed. So we have we will see one example zero division error and how to write a try and accept block in Python. So this is Python. Suppose I will take two numbers from the user a equal to int then input then enter this is number one then b equal to int input then enter number two then result equal to a divided by b and then print result equal to comma result. This is your common program. But how to include now try and accept block inside this program. Generally you know that exception may occurs for this line for this statement. So what I will write I will write this inside try block. So this is try. This is my try block and then accept block. So this is accept. This is print. I am printing error message. This division by zero is not possible. This is try and accept block. We will execute this. Suppose enter number one, number one is suppose ten, number two is suppose five. So output is two. So number one is ten, number two is five obviously division is possible. So result will be calculated as two and it is printed. No accept block will be executed. What happens suppose I will give number two as zero suppose number one as ten and number two as zero. Now when I have entered number two as zero zero will be stored inside b. So result equal to a divided by b so ten divided by zero it is not possible not possible. So this is an exception. Exception occurred for this statement. So next statement is not executed control will automatically jump to the accept block in this case. Next is whatever statements you have written inside accept block that will be executed. So in accept block there is only one print statement that will be printed. So generally try and accept block generally you are writing you are including in your program to handle the exception. If exception is occurring and still you want to print some messages to the user or you want to handle the program or to close the connections of database or to close the collections of files generally that's why you are writing and try and accept block. We will move forward. Next one apart from try and accept there is finally block is also present. So Python provides an optional final statement which is used with the try and accept block. It is executed no matter what exception occurs and used to release the external resources. The finally block provides guarantee of execution. So syntax is like the try then code accept then code then finally then code. As we know if exception is not occurring in the try block accept block will not be executed. If exception is occurred in the try block somewhere so all the statements after that that statement will not be executed control will directly jump to the accept block. What finally block is there finally all the statements inside finally block will be executed irrespective of whether exception occurring during execution of your program or not. So what code you write generally inside finally block whatever connections which are related to database or related to files generally you are closing statements you are writing inside a finally block. So irrespective of whether exception may occur in the try block or may not occur a finally block will be executed. So this is about finally block then accept statement using with the exception variable. So you can declare a variable for exception also for printing what type of exception is there you can use the variable also. In this case suppose this is a try block and this is accept exception as E. So exception it is all type of exception may handled by this exception class. Instead of writing divide by 0 error or IO error something like that generally you write exception. It will handle all type of exceptions. So exceptions as exception as E. So E is a variable name in this case. So I have printed a kind divide by 0 and then print E. So E is a variable name it will print what type of exception occurred during execution of your program. So you can store the exception inside a variable also in using this way. So think and write you can pause the video and you can answer for this question. Gaze the output if A equal to 0 and B equal to 0. This is try block A equal to int input enter A, B equal to int input enter B. Suppose A equal to 0 and B equal to 0, C equal to A divide by B, print A divide by B accept exception print can't divide by 0 then finally block is there. You can pause the video and you can answer. This question is value of B is 0 exception occurs for this statement control will jump to the accept block it will print can't divide by 0 after accept finally block will be executed. So it will print program ends here. So output for this program is can't divide by 0 it is a statement which belongs to accept block and program ends here it is a statement which belongs to finally block. So this is output of this program. So these are the references I have used for this video. Thank you.