What are exactly the Python Try Except statements?
Python try except statements are useful in handling errors that occur in python code. The try block is used to try a block of code and if that doesn’t work, the program falls back and runs the except block. There are a few other bits to it but we will explore that through examples.
In this example, we are going to create a function that lets us add two numbers together
So we’re going to define adding x and y. And from this function, we’re going to return x + y. So let’s say we want to print out the add function and we want to give it arguments one and two. So we’re expecting to see three as the output when we run the code.
What if we were trying to say programmatically and we wanted to add numbers together and we actually ended up somehow with a string involved in the numbers that we are passing in as arguments. Maybe from user input or from a list that we’ve got elsewhere. As shown below.
We’re going to get a TypeError that lets us know that we have used an unsupported operand type. So what that means is we can’t add a number and a word together, which makes sense.
But what we can do is we can actually put this into a Python try except block. That will let us manage that error and not stop our code dead in its tracks.
How to use the Python try except to avoid the error?
Just after the return statement we can then do away with the print statement and create a Python try except block. We will begin by writing the word try and then a colon and then we indent our code in the next line. Now since we have the try word, we should have the except word, otherwise, the code will fail.
So then back and then on the same line as our “try” we’re going to write the word except again a colon and then we hit enter and then write the word pass in the next line.
Now, this code is basically just going to pass through if it finds an error.
So what we’re going to do now is we’re going to change the argument “sheep” back to the number 5. So we’re going to expect to see 6.
And then if we pass a string as an argument, we’re going to get nothing out. And that’s because it has run through this try and it’s gone. Can we add one and a word together?/No, we can’t. So we’re going to do the Python try except block instead.
Instead of using pass let us now write some code that the program can execute instead. We’re going to run the code that prints the statement “not a number”.So now when we run this because you can’t add a number and a string together, it’s going to execute the code in this except block. So we’re going to get “not a number” as the output.
If we change the argument “sheep” back to a number let say 5, it’s going to run the code in the try block. Since it was successful, it doesn’t run the code in the except block.
The use of else in the Python try except
So what we can also do is we can throw in the word else. So if we put else on the same line as Python try except, we can say else and let’s print a statement that says the addition is complete. What this is going to do, is if “try” code works, it will also run the else.
So now when I run this, we can see that we have the number seven, which is the complete edition and we also have the words “addition complete”.But if we change this back to a string, we’re not going to see the output “addition complete” because it failed on the try block. So since the try block of code failed we only run the except code and then it’s done. The else is completely ignored unless the “try” is successful.
The use of finally in the Python try except
What we can also do is the final one is we can write finally. What this is going to do is this code block will always run when this is executed. So this is quite useful in certain cases. In this example let’s add a print statement after the word finally that says “end of program”.What happens here is the “finally” block of code will always run regardless of the outcome of the Python try except so again, we’re going to run this and we can see that we have “got not a number” and the “program finished”.
So we get “not a number” since the except code block was executed because the code under the try block doesn’t work the else code was not run, but we did execute the “finally” block of code because that is always executed.
So let’s change it back again and let’s just quickly add a number as an argument instead of ‘cat’ so we’ll see that we get 11, which is the output of our function. The “addition was complete” and the “ program was finished”.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.