Python Tutorial for Beginners 33 - Python Exception handling + Python Try Except



 hey guys welcome to the next blog in Python tutorial for beginners in this blog we will learn how to use exception handling in Python so first of all what are exceptions so an exception is an event which occurs during the execution of a program that disrupts the normal flow of the program so basically exception is an unexpected event which disrupts the normal flow of the program so let me show you some examples of exceptions which may occur during the execution of a program so to start with I'm going to open the pycharm Python consoles and then I'm going to maximize this PyCharm Python control and let me also just clear this terminal so we will start from the top so let's say we have a number and we divide this number by 0 and when we press ENTER it's going to give us this exception which says zero division error so this zero division error is an exception when you divide anything by zero in Python let me show you some more examples so let's say I want to add a number and a string let's say 10 plus 10 but this then is an integer and this 10 is a string and when I run the command it says type error unsupported operand types for int and string so we cannot add a number and a string in Python so this type error is another kind of exception let's say I'm going to just use a variable name a b c which i haven't declared till now and when i press enter it's going to once again give me this exception which says name error name ABC is not defined which is true because we haven't defined the name ABC but we are calling this variable which we haven't defined so we this exception which says name error next let me define a variable and I'm going to assign some couple values here one and two and now what I want to do is I want to call some methods so you can see some methods are available for this tuple now let's call some random method using this double variable it will give us an error which says attribute error double object has no attribute whatever attribute I have provided here so this attribute is not present in the tupple object also when you call a method let me call some other method which is not there and once again when I press ENTER once again I get this error which says attribute error because this function is not present in the tupple object so these are all exceptions in Python attribute error or name error or type error or zero division error all are exceptions in Python now if you want to see more exceptions you can use the help inbuilt method and then you can write built-ins and then press Enter it gives me the name error once again because I need to first of all import this built-in module so import built-ins and I'm going to once again use help and inside the parentheses built-in and then press Enter which is going to give me this kind of output and when I go at the top of this output so let me just scroll at the top and here you will see under the classes there is an object class and under the object class there is a base exception class and under the base exception comes the exception class and under the exception there is a big list of exceptions which you can see here so 0 division error we have already seen or when you scroll down name error we have already seen so all the exceptions which Python may throw are all listed here so if you want to see the list of all the exceptions you can just give this method which is help built-in but before that you need to import this built-in module and then it's going to show you this kind of output and you will also be able to see all these exceptions in Python now let me show you a real life example of exceptions in Python so what I'm going to do is I'm going to define some variables here so first variable is result and I'm going to initialize it with the value none and then I'm going to declare two variables let's say a which takes an input and we want to ask the user to provide some numbers so let's say number one using the variable a and in the same way I'm going to declare a variable B which also asked the user to provide a second number but you already know that this input will return the string so we can just convert this string into a float let's say so I'm going to just cast the output output of this input string to a float value and then what I want to do is I want to save the division of the two numbers a and B into the result so a divided by B I want to save it into the result and then at last I would just want to print the value of the result now let's run this program so right click run the file name and then let me provide some numbers here so 5 and 6 and press Enter it's going to give me the result which is the division of these two numbers let me run the program once again and this time I will provide 10 and the second number is equal to 0 here and we know that when we divide some number y 0 we get this exception which says zero division error and you will also observe that as soon as this error is thrown so this error comes from this line line 5 so when you click on this line it shows us that this is coming from this line which is the division of these two numbers and as soon as this exception occurs all the code which we want to execute after that will not be executed so we were printing this result here but it's not printed let me print something else here so I'm going to just say end and once again I'm going to run the program and I'm going to provide the same inputs here and you can see neither this statement is printed nor the end is printed so whenever an exception occurs title stops whatever it is doing so in our case whenever this exception is thrown at this line both these statements will not be executed and why these two lines of code will not be executed because we haven't handled this exception so Python allows us to handle the exception which we are not doing right now that's why it stops everything whenever this exception occurs so how to handle these type of exceptions so you can handle these type of exceptions using a try accept keyword so just write try and then under the try you write whatever you want to test and then you write accept keyword here so just write accept and then under the accept you write some code which you perform when the exception is thrown so I'm going to just say this error which is float division error okay so this is what I'm going to print and don't forget to put colon in front of accept and try so let me run the code and let me give the same condition so 10 and second is 0 and this time you will see that this exception is not thrown and the result is also printed and the end line is also printed you can see here and also this line is executed which comes from the accept statement which says float division error so whenever you use try/except notation first of all your code under a tribe will be evaluated and if it throws some exception then whatever code you write under the accept will be executed and then all the other statements will be executed whatever comes after this try except code so this time no error is thrown and we have handled this error and you can see clearly it says that flow division by zero which means it comes to the accept condition so now we are handling this error which is zero division error now in other condition when this error is not thrown so we will just give some valid value ten and five so no zero division error will be thrown and when I press ENTER you will see that result will be printed and and will be printed by these two lines but no accept statement will be printed so this statement is not executed because this code has not thrown any error because there is no error occurred now as I have shown you that all the exceptions are under exception class so we can also write here exception and then we can just write this notation exception as E and this will show us more details of the error okay so I'm going to just write here error is equal to and then I'm going to just print whatever the content of e is and once again when I run the code and I will give the number it's the zero here and you will see it prints this error which says float division by zero so this is printed using this iki word which shows us the correct error I have also shown you that this exception is the base class of zero division error for example when I will try to know the type of there at E and when I run the code once again and let me provide this input once again you will see that this error is of type zero division error so we know that this exception is thrown from this class so instead of writing this generic exception class we can also use this zero division error here and now whenever this specific error is thrown then only this will be printed here so let me run the code once again and let me give this input here and you will see once again this line of code is printed using this statement and that means zero division error is caught by this line of code and then the statement under that is printed now let's say this line of code throws some other kind of error other than zero division error so let me just remove this typecasting or float here that means it's going to throw us some other kind of error which is not zero division error and let me run the code now and I will provide two numbers here once again and you will see that now this error is different which is type error which says unsupported type string and float so why is this happening we are catching the exception using this except keyword but we are only catching the zero division exception we are not catching that type error exception that's why our program is stopped and these two lines of code is not executed so you need to either use the base class of the exception which is this one exception and when you run the code and when you write these input it will be handle and you can see this time type error is caught it's not a zero division error but the type error which comes because the type which we have provided here is string and we are dividing string by a float value which throws this error so if you want catch all the exceptions you use this base class but it's not a good practice you need to have information about your exceptions so I'm going to once again write this zero exception error here and what I can do more here is I can add one more except statement here and this time instead of zero division error I can say catch also type error okay so I'm going to just write type error and here zero division error so we know from where this error is coming and when I run the code once again and when I give the inputs you will see that this error is coming from this line of code so this except statement is catching your exception which is type error now when I just add this typecast of float once again and when I run the code once again and when I provide ten and zero once again you will see this error is caught by the other accept statement which is this statement which is used to catch the zero division error so you can also provide multiple accept statement in order to catch multiple exceptions in Python so this is how you can do exception handling in Python there is more to exception handling which I will cover in the next blog so please stay tuned and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News