Python Tutorial for Beginners 34 - Try Except Else Finally (Python Exception handling)

 


hey guys welcome to the next blog on Python to troll for beginners in the last blog we have seen how to use exception handling in Python and we have seen how to use this try accept statement in order to catch the exceptions now in this blog also we will talk more about exception handling in Python and we will talk about else and finally statement with this try/except statement so I will be using the same code which I have used in the last blog so I will highly recommend you to watch the last blog if you haven't seen that now to start with at the end of this try except statement we can also use the else statement so you can just write else and for now I will just print here for example underscore underscore else underscore underscore and also we can use a keyword called finally with our try except statement and here also I am going to just print underscore underscore finally underscore underscore so what does this else and finally statement do let's see by running our program so I'm going to run the program and now I'm going to give some valid input so that our program will not throw any error so I'm going to just divide 10 by 5 and then press ENTER and you will see what is printed here so first of all else is printed then finally is printed and then the result is printed using this print statement and then at the end this and string is printed so in this scenario there was no exception thrown by the code that's why none of these exception statements are printed so you can use L statement whenever your code does not throw any exception so whenever your code doesn't throw any exception else statement will be executed but when this finally statement is executed so let me give you one more are you and then we will discuss about the finally statement and this time I'm going to intentionally write zero here which is going to throw the exception and I'm going to press Enter and you can see zero division error is thrown which is caught here and this statement is printed first and then this finally keyword is printed using this statement and at the end these two lines of code are executed so once again let's talk about the else statement you can see whenever your code throws an exception then this else statement will not be executed the else statement will only be executed whenever your code doesn't throw any exception but what's with finally statement so we have seen in the case of exception this finally is printed and in the case of no exception also this finally is printed so this finally statement is guaranteed to be executed whether exception occurs or it does not occurs so the only code which is guaranteed to be executed in all these lines is the finally statement finally statement is always executed whether your code throws any error or it doesn't throw any error now in which cases you might want to use this finally statement so sometimes whenever you use files you need to close these files after opening them whether the exception occurs or not it is mandatory to close your files and in those cases you can use this finally statement the next example is for example you are using any database and you are opening that database right and for some reason your connection is lost with your database then the exception is thrown and in the finally statement you can reconnect to your database using the reconnect code of the database so in finally statement you would want to execute that code which you think that have to be executed whether error happens or does not happens so in all those scenarios you can use the finally statement now the next question arises is whether we can use this else and the finally statement without the accept statement the answer is you can already see here no so you cannot use this else statement without the accept statement so you have to use accept in order to use this else statement but when I remove the accept and else statements you can see there is no error now so you can use the try and finally without the use of accept or the else statement because the code under finally will always be executed whether your code throws any error or not so it's independent of this accept statement or the else statement so this is how you can use else and finally statements with the try and accept key words I hope you have learned something new this time I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News