1.17 Exception Handling in Python | Python Full course 2024 #learnpythonin2024 #mlwithakansha

Akansha Yadav covers Python's exception handling with try, except, else, and finally blocks. She walks through a division example that catches ValueError for non-numeric input and ZeroDivisionError when dividing by zero, showing how each block executes in different scenarios.
Chapters
Ladies and gentlemen, in today's video we are going to see what is exception handling in Python. So let's first see that what exactly this word means. So if I'm saying exceptions, means there are multiple exceptions in different, different scenarios. So in programming also we do have some exceptions that we want our Python to handle. So this is a mechanic mechanism, we can say, that allows you to handle errors or exceptions that might occur in your code. So it, it gracefully can be handled using try and except. So there is one block called try and except. I'm going to write it down on my screen over here that how it looks like. So this is how it looks like. So first is the try. Try is the keyword in Python. After writing try, you will write what code which, which you think that might can raise an error. Or raise any exception. So that you can write after try. Then you can say that accept this particular exception as e, code to handle that exception. Then else, what to do if no exception is raised? Suppose there is no exception in the code. I was thinking that that might occur, but that is not there. So else, what should do? Finally, that code will be executed no matter whether an like exception occurred or not. So this is basically a structure through which you can handle the exception handling in Python. So for handling exception, like, you will use this try, except, else, final. 4 things you have to use. 4 are— all 4 are the keywords: try, except, else, and final. So we'll see with an example. I will quickly write one code on the screen. First, let's understand one by one what is try block. Try block is the place where you write the code that might raise an exception. Second block that we have is the except block. The except block is if an exception occurs inside the try block, the code inside the except block is executed. If the execution occurs, the except block will take the charge and will execute it, whatever is being added after except. Then there is one else block. This is an optional block, this is not must to add, but if you have any exception, if you feel that this ex— this exception might not occur, then in that case I wanted to continue, or whatever you want. You will write if no exception were raised in the try block. It is an optional block, you have to implement it. Another is the finally block. Finally is also optional block. So basically this is the name— the entire process name is the try and except only, even though you can use two more keywords, which is else and finally. So let's write an example for the same. I will write one for you, and so I will just add one more cell. Okay, so what I have written till now is try num1, num2, and result. Num1 and num2 are two variables through which I am taking numbers. Number 1 and number 2, I am taking both the numbers in integer, and here also I'm taking it in integer. Then I'm trying to, uh, save the division between num1 and num2. num1 is divided by num2, and I am saving that division in result block. After that, I am thinking that there can be any value error. Maybe instead of a number, the user is typing any letter. So that can— might raise an error. So in that case, I am using a ValueError. ValueError means there can be any value. There can be one scenario where output is— suppose first number is 0, another number is 1. So 0 cannot be divided by 1. So in that case, it might give you ZeroDivisionError. These are predefined errors. There are multiple errors similar to this. But I am showing you with the two— ValueError and ZeroDivisionError. When you have ValueError, I am saying that print and please enter valid number. And then I'm— if the execution, like, in within the try block, whatever I have written is not raising any error, then in that case the except block will work. This else block will work. So in case of ValueError, it will go with this one. In case of ZeroDivisionError, it will go with this one saying that can't divide by zero. In case of like none of these, like everything works fine, then in that case it will print that result. Finally, it will say that this block always executes. So it says a number, so I'm writing 5. Now I'm writing a, it gives me an error. This block says always— like, it first is please enter valid number, then it says that this block always executes. Basically what I am trying it to do is I'm trying it to print that particular statement once the try and except block is completed. In that case only I'm trying to print it. And I'm going to run it again. Let's suppose I'm saying 0, second number is 1. So it says result is 0.00. So in that case, 0 was divided. Let's run it again. So now we get can't divide by 0 because in this case I have entered numerator as 1 and denominator as 0 because can't divide by 0. We cannot divide any number by 0. So that is what it is trying to do. So exception handling is also one of the key concepts in Python. You must understand it carefully. We'll meet in the next session. Till then, you can watch other recordings. And if you have anything to share with us, you can comment down, or you can check out our website also, www.learnmlpython.com. We do have two websites, www.thelearningskills.com. You can go and check out those websites and blogs. We'll meet in the next session.










