There are different statements in Python. In this lesson, we will focus on some of them, Python try and except statements. Finally, we will also learn finally statement.
With try statement, we can test a code if it is working fine or not. In other words, we learn if our code will return an error or not with the help of try statement of python.
On the other hand, except statement is used what the code will return if are there any error in the code.
Lastly, finally statement let us to execute the code independent from the result of the code.
Now, let’s start to see how to use Python try and except statements with examples.
As you know, firstly, we should define a variable to print it. If we try to print a variable without its definition, the code will return an error. Below, we will use this print function without variable definition with the help of try statement. Beside we will use except statement to do something if this code return an error. Here, it will return an error.
Firstly, let’s try to print a defined variable with the help of try statement. Here, we are trying if this code is working or not.
The return of this code will be:
You can also watch the video of this lesson!
Now, let’s learn what will happen, if we do not define the variable a.
The return of this code will be:
We can also use except statement multiple times. By doing this, we can specify the error type and for this error type we can do a specific job. Below, there is an example of this multiple except statement usage.
The return of this command is:
If we use another error type, then it will go to the other except.
Now, let’s learn another statement that is used with Python try and except statements. This statement is finally statement. Finally statement executes the code evet it has an error or not. In other words, we specify a code with finally statement and then it executes both the result of try and except code and then finally code.
Let’s do an example and understand better, how finally statement works.
The output of this python code will be:
Now, let’s see how to do anything, if there is no error in the code. Here, we will use else statement to do anything after the code executed.
The return of this code will be like below:
Here, we have defined variable a, so, print function will work in try statement. After that, because of the fact that there is no Error, else statement will also work and it will print “All is Good!”.
We can write a code that will raise an error if something happens. For example we can control the value of a variable and if it is higher than a specific value, then we can raise an error.
Below, we will control the value of a variable and if it is higher than a value, then we will raise an exception with raise statement.
The output will be:
As you can see, it is raised as exception and the string that we have mentioned are printed also.
With raise statement, we can also define the error type that we will rise. Here, we have raised an exception, this can be also a TypeError, SystemError, SyntaxError etc.
Here, we have talked about Python try and except statements, finally statement and else statement. We have also learned how to use, raise statement to raise an error according to a condition.