#25: Python Exception Handling: try...catch...finally | Python for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i'm sure you have run into errors numerous times while programming basically these errors are of two types one is syntax errors like missing parenthesis wrong indentation etc you can easily fix these errors by fixing the syntax and the other type of error is exception even if your code is syntactically correct it may sometimes result in an error for example if you divide a number by zero you will get an error these types of errors that we encounter during the runtime of the program are called exceptions in this video we will learn about exceptions in detail and then we will see how we can handle them in python so let's get started let's understand about exceptions first for this i will write a program that will give us an error so in my code editor i'll say numerator equals 10 denominator equals 0 now i'll say print numerator divided by denominator and i'll save this file let me run it i'll say python main dot pi and you can see that when i run the code we can see the zero division error has been raised even though our code was correct syntax wise it's not allowed to divide a number by zero in python this is an exception in this case we are getting the zero division error exception depending upon the error these exceptions can be of different types for example if you try to access a file that doesn't exist we will get the file not found error exception similarly if we try to access the item of a list out of range we will get the index error exception now that we know what exceptions are we will see how we can handle them next and by the way if you're finding this video useful a sub to the channel would be much appreciated as we know when our program encounters an exception our code ends abruptly with an error message and most of the time rather than showing the default message we may want to show a custom message that's more helpful or run a different set of code this is known as exception handling it's the process of responding to exceptions in a custom way during the execution of a program in python we use the try except block to handle exceptions and its syntax goes like this inside the try block we write the code that might throw an exception now if an exception occurs the control of the program jumps immediately to the accept blocks and the program continues and if exceptions don't occur the except block is completely skipped pretty simple right now let's see a working example of it so inside the try block i will write some code that might raise an exception let me remove this and here i'll say numerator equals integer input enter numerator then i'll say denominator equals integer input enter denominator next i'll say result equals numerator divided by denominator and let me print the result here we are taking numerator and denominator from the user dividing the numerator and the by the denominator and then printing the result this code may raise an exception if the user enters 0 as a denominator let's print a simple message inside the accept block if an exception occurs so i'll remove this code and here i'll say print denominator cannot be zero please try again let me also put another print statement outside the try accept blocks here i'll say print program ends let me save this and when i run this so i'll go to my terminal and say python main dot pi in the numerator i'll enter 10 in the denominator i'll enter 0 and as you can see denominator cannot be 0 please try again is printed this is because this line result equals numerator divided by denominator raises an exception when denominator is zero in this case the control of the program jumps to the except block and the code inside it is executed let me run this program again so here i'll say python main.pi and this time let me enter the numerator as 4 and denominator as 2. when i press enter as you can see the correct answer which is 4 divided by 2 is printed and the accept block is not executed this time before moving to the next section of the video the program is team has created an app that allows you to learn python from your phone the app contains bit size lessons that are easier to understand a built-in interpreter so that you can run python on your phone quizzes and many more the app is available on both ios and android the links are in the video description i have this code from our early example on my screen here it doesn't matter what type of exception it is this except block is executed whenever an exception is thrown it's also possible to handle different types of exceptions in different ways for example we may want to print different error messages for zero division error and index error exceptions we can do that by specifying the type of exception after the accept keyword so here after accept i will add zero division error now this accept block is only handling the zero division error let me run this code so here i'll clear this and let's say python main dot pi enter numerator 10 and enter denominator again i'll enter 0 because i want to throw the exception and as you can see the code inside the except block was executed handling specific exceptions in this way is particularly useful if our try block may raise more than one type of exception suppose our try block can raise the zero division error and the index error as well we want to handle these two exceptions separately we can do that by adding another except block to handle the index error exceptions separately let me show you what i mean by this i will add code inside the try block that may raise the index error exception so in my code after the print result here i'll say my underscore list equals 1 comma 2 comma 3 and i'll accept input or the index from the user so here i'll say i equals integer input enter index and then i'll print my underscore list i this code will raise the index exception if the user enters an index greater than 2 because our list only has three items now to handle the exception i will type another except block so here i'll say accept index error and inside this i'll say print index cannot be greater than size of list let me add the full stop now when i run this code i'll say python main dot pi oops there's an error so the error seems to be print result no print oh i forgot a bracket here okay i'll save this and let me say python main dot pi it says enter numerator so let me enter something like 10 in the denominator i'll enter 5 now when it says enter index let me enter something like 5 when i press enter then you can see that index cannot be greater than size of list which is the custom message i had put in for the index error is printed a try statement can also have an optional finally block which is executed regardless of whether an exception occurs or not and its syntax goes like this let me run this code first so here i'll say python mean dot pi now let's understand what's going on in this program this block of code gives us the zero division error so the except block denominator is printed to the screen finally the code inside the finally block is also executed however if an exception doesn't occur in the try block this except block is not executed but this finally block is still executed the final block is usually used to perform cleanup actions that need to be executed under all circumstances suppose we are working with an external file in our program we need to close this file at the end even if there was an error while writing to it in this case we put the close file function inside the finally block at this point we have covered pretty much everything we need to know about exception handling by the way we can also create custom exceptions in python if you're interested you can check that article on our website programmings.com the link will be in the video description that's it for this video if you want to revise this concept you can find all these programs in our github repository i'll also put the link in the video description and if you like this video hit that like button and subscribe to the channel and i'll see you in the next one happy programming you
Info
Channel: Programiz
Views: 19,772
Rating: 4.9587631 out of 5
Keywords: #25: Python Exception Handling: try...catch...finally | Python for Beginners, python tutorial, python exception handling, python programming, programiz, programiz python, programiz python examples, programiz python programs, exception handling in python, exception handling, try catch python, Python for Beginners, python error handling, python error, python errors and exceptions, ZeroDivisionError, Python Exceptions, python custom exceptions, python, python tutorail, programming
Id: brICUKrzVR0
Channel Id: undefined
Length: 9min 37sec (577 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.