#63 Python Tutorial for Beginners | Exception Handling

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens my name is Debbie 20 and let's continue this series on Python the till is fine we have talked about oops coins that we have talked about the fundamentals of Python now let's go towards some negative stuff which are errors of course I know it likes error but then if you talk about this error we have three types of errors in general the first one is compile-time error the second one is a logical error and the third one is a runtime error now what it means now let's say if you line up code and that code has multiple statement in one of this statement you have a syntax atom let's say we feel working with if and then you forgot the colon then if you are using print with the wrong spelling so if you make those type of mistakes those are syntactical errors right and you can call them as compile time errors because you will be getting those errors at compile time the next set of errors is logical errors now what is logical so let's say your code is working so there is nothing wrong with the syntax everything is working but then when I user says hey I want to add two numbers two plus three and the other four two plus three is fine right but let's say if I uses getting full so when you say two plus three if the answer is four that's a logical errors and logical errors gets compiled your code also gives you the output but the problem is it will not be a correct output and that's logical error the third one we have is runtime error so in one time either your code gets compiled what is wrong with that there is no syntactical error your code is not actually giving you a logical error as well everything is working ok so when a user says 2+2 it is giving you food sometimes you know if I user might give you a wrong input example let's say if you are going to divide two numbers 5/6 okay so both the values are coming from a user right so you are trying to divide two numbers which are given by the user if I user says 6 + 2 it works right it will give you 3 if I use a size 10 and 5 you know it is 2 again but then what if a user says 6 + 0 you cannot divide a number by 0 right and that's where it will give you an error now this is not a compiler because the code is containing by the properly this is not a logical error because for normal execution it works it is not working for specific any but it is not working for I mean your code was working somewhere between your getting error and that's your runtime error because you are getting error at runtime now out of this which is the most easiest order to find the compile time right because at the compile time itself at the developer level at the developer site itself you are getting errors logical errors are again is it to handle because as it will probably also test the application right all we have a specific testing team for that so we do test this software for different bugs so logical and just normally comes in the bugs as well what about runtime error because see one time either the mistake is not done by you the mistake is done by the user right so if you are saying hey user 82 values and in your mind you have that a user will not enter zero value for the second one but user is doing that maybe this application is using certain file from a seed drive so it was working from last seven days and today morning user has deleted that file and now if you open the adaptation will give you either because it is not able to find the file right so all these things comes under runtime error so you aren't thinking that's okay right that's a user issue user is not able to handle this of a properly see first of all as a developer you have to take care of all those things you have to also understand what mistakes our user can do maybe not exactly a user but maybe there might be a sugar the database connection there might be issue with the network connection so if you are using an application which interacts with this server and maybe there is no internet then what will happen your software should not get stopped they're going to get runtime error your execution stops and that's an issue banking software so that's saying we have millions of transaction on the server and there's only one runtime error because of which your software will stop working and you will lose billions of dollars so the important point is even if you are getting the exception even if you are getting the error your execution should not stop that's our main logic here let me show you in the example here what I will do is I will try to use two variables here I will say a is equal to 5 and B is equal to 2 and of course if we divide this 2 numbers it will work let's try so I will say print a divided by B now in this code if you can see we have three statements right we have line number three which is statement one line number four with a statement to line number six is statement three now out of these three statements you can divide the statement two parts one is normal statement and then we have critical statement normal statement means which will not give you at an example when you say equal to five there is no problem with that when you say be able to do there's no problem with that right even in future if you want to change the value from five to something else it will not give you error what's with the line number six so what will happen with line number six it will execute for this code let me try it so I will right click here I will say it one and you can see it works you've got the outer image two point five and let's say after doing this let me also print a statement which is by so that means after printing the value I am printing by as well and now I run this code you can see we are getting the output and then we are getting by as well now what if if I change the value of B to 0 C of course we know that is not possible you're going to divide a number by zero the moment you do that you will get an error and of course the error it says there was a trace back and then it was line number six okay that's right like number six we got the error it will also print this statement which is giving you the data and it also shows whatever it is and it is zero division error so this is your message division by zero not possible right so how do we solve this so there are two problems here the first one is user will not understand what this error means right second column is you can see we are not getting by in the output so in the output is no pie that means your execution is getting stopped in between we don't want that so if you want to solve this problem we have to use a special block C at this kind of code we are getting exceptions but we are getting error so what you can do is what your block you have you just need to put that in a dry block or in a dry statement so we have a special thing called as dry so what we are saying hey this line of code this is a critical statement and not sure if this code will work or not so try to execute that okay so try to execute so your Python will say okay since you are writing this in a try block I will try to execute but what if there's an error then you will say oh okay if there is an error it's very simple I will accept the error okay I will handle it as a programmer and the way you do that is by letting exception so you have to say accept exception now the exception so the moment you get an edit this line this will give you the error and this is where you will handle it now what type of hand thing we want here how can you can do anything you want example so if you know that it is because of a divided by B you can simply say hey you cannot divide a number by 0 and trust me this message is much better than the error which we are showing before right so now if a user is giving you the value which is 0 for the second value you will print a message by saying hey you cannot divide a number by 0 you can't print any message you want in fact you cannot just print a message you can do whatever things you need example if you want to perform some operation here you can do that at example if you want to multiply two numbers in what you add two numbers you can perform different operations here as well we don't want that let's keep it simple so in the exit block you will print the message and now let's run this code and you can see it works you can see we got hey you cannot divide a number by zero and it's printing by as well so this works right so what we are doing is we are trying to execute this statement if this works we'll print it otherwise we will print the except block okay that works what if if I change the value to two again will it print hey you cannot divide by zero okay let's go with the flow now so what will happen you got the values which is five and two you're saying hey try to divide these two numbers you are saying 5 divided by 2 there is no error okay there is no error the moment you don't have any error it will jump out of the block and I mean it will continue the block first of all it will not execute the except block because except the OP will execute only when you have an error ok that's why you can see if I on this code we will not be getting any add up as number by 0 okay but what if you want to print the message error as well what if you want to print what is the error so what you can do is here you can say exception as E so that you can represent that exception object in some way so this e is just a representation or the object of exception and here I will try to print e as well just to see what is happening inside E and I will go back to my value to zero let's run this code and you can see it is printing the message the message which we have plus it is also printing the exception message which is division by zero okay so that's how it works that's how you can handle exception ah one more thing to talk about let's say I'm not printing by here what if I have a special thing I'm opening a file example you know it is always a good practice when you work with files when you open a file you should be closing that file if you open a database connection you should be closing that connection before exiting okay that's what we do right so every time you open a fridge to take out a bottle you make sure that you close it now even if you get the exception let's say if you're opening of fridge and someone is calling you now you will make sure that you close the fridge right you don't leave the fridge open and go somewhere right so that means if any would you open as a resource always close it hold on let's say if I open are the source inside a try block so let's say if I'm opening up failure if I opening a database connection here where should we close it should I close the resource inside try I can print a message again the message is not important what is important is we are reading to some statements so that we can close the resource since we don't have any source as of now I would simply go with print statement so here I will say resource open see this is the source can be anything okay this can be a file this can be a database okay and here I will say resource closed okay this works if you have a different input like five and two this should work you can see it says resource open you got the output and says resource closed but what if you get the exception now in this case when you're on this code we have resource open that works it is printing the message but can see that we don't have a source close what went wrong the thing is the moment you get the exception at this line you are getting exception right it is jumping outside this statement it is jumping outside try is going to accept log so that means issue of typing it here which should be doing that inside the exception something like this okay so should be closed in a sauce inside except block so if you're on this code it works you can see that we are saying resource open there was the exception but we are saying resource closed but now what if I change the value from zero to two in this case if you concentrate you can see it is not saying it's just closed because you will not be executing the exit block if you don't have the error so we have problem so should I put resource close both the side in try and accept as well we don't have to because Python provides you one more feature of it spoiled finally notice finally means you can put your statements and finally so finally simply means it doesn't matter if you are getting the exception or not I will execute so if you've got the exception don't worry I will execute myself even if you don't get the exception that's fine I will execute so accept block will be executed only when there's an error finally we can execute that doesn't matter if you're getting the exception or not beauty right and that's how you work with try accept and finally so point remember when you work on a statement your statements might give you some exceptions so try to handle them with the help of try accept and finally in fact we can do one more thing here what if if I take a value from the user and let's say we have a variable K and the value from the user I'm taking with the help of input and the type of input I want is of course integer so I will say enter number and then to convert that into integer we will be using ain't function right and then maybe I want to print the value of K that's it nothing much the moment you learn this code you can see it says resource open and now we also got the output which is 2.5 which is very fine number I will enter number which is 6 everything works there's no error we got number 6 as it is because we're taking it and then we are saying resource close let me change the value let me change the value from let's say P now that the issue right we are saying enter number then we are saying P oh we got an error it says you cannot divide a number by 0 but that's weird we're not dividing by 0 and then it says invalid literal because this is a message which is printed by ear remember the e is printing it right it says in literal 4:8 with base 10 or that's fear what if I put that outside they try I just want to see what about a ray throws because see when you say / 0 it says 0 division error remember here if I print this I just want to see what errors it gives you if I say P and you can see the error name is value error so for different type of errors we have a different names as well and the jungle one is exceptions so exception will be on top so even if you say zero division error it's a part of exception value what is a part of exception so exception can't handle everything but you can be specific what you can say hey I want to say you cannot even a number by zero when there is a zero division error so only when there's an error of zero division error you'll trenders example you know when we ask sake when you are not feeling well you will go to opportu good doctor example if you something wrong with your let's say nose you will go to ENT doctor if you take something wrong with your let's say skin you will go to a skin doctor if there's something wrong with your teeth you will go to a dentist right same way for different type of errors we have different above except block so you will say except and this time you look off a value error and you will say again as it doesn't matter it's just a name of the object okay you can have any name just doesn't matter there and you can print 10 valid and put that's it just a different message but what if there's an error which you don't know example you are trying to handle 0 division error you're trying to handle value error what if there's an error which you don't know about in that case at the end to be on safe side you have accept and you will say exception now this exception will try to handle all the other errors are not 0 division and value error it's something like we have dentist we have gynecologist we have skin doctor and then we have a general doctor we can handle everything the same way this exception can handle all the exceptions and here you can print something went wrong yeah that's it you can print the message you want and now I feel on this code with a different value of course if I say P now you can see oh we got an error we still got an error because I forgot one thing I'm still doing this outside drive during that inside drive remember and now let's run this code let's say entire value will just be do you see that it says invalid input so for different type of adders you have to use different except block see if you're doing this for the first time it will take time okay so it takes time so do practice we try and accept like you're coming from Java background we do have this concept in Java as well right let's try carriage and finally the same thing so I hope you enjoy this session if you have learned something dope you're like button there and do subscribe of other videos and most important do share this video with your friends it might be helpful for them so let's everyone bye bye
Info
Channel: Telusko
Views: 277,577
Rating: 4.9570041 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, begginer
Id: 6SPDvPK38tw
Channel Id: undefined
Length: 15min 59sec (959 seconds)
Published: Sun Sep 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.