Python Try and Except

python-try-and-except-ipcisco-1

There are different statements in Python Coding. 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.

 


You can check also Python If Else Condition lesson.


 

Try and Except Statements

 

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.

 

try:
  x=5
  print(x)
except:
  print("An Error!")

 

The return of this code will be:

 

5

 

You can also watch the video of this lesson!

 


You can also check Python For Loop


 

Now, let’s learn what will happen, if we do not define the variable a.

 

try:
  print(a)
except:
  print("An Error!")

 

The return of this code will be:

 

An Error!

 

python-try-and-except-ipcisco-1

 

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.

 

try:
  print(a)
except NameError:
  print("a is not defined!")
except:
  print("There is another Error!")

 

The return of this command is:

 

a is not defined!

 

If we use another error type, then it will go to the other except.

 

try:
  print(a)
except TypeError:
  print("a is not defined!")
except:
  print("There is another Error!")

 

There is another Error!

 


 

Finally Statement

 

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.

 

try:
  print(a)
except:
  print("An Error!")
finally:
  print(“I will close the program.”)

 

The output of this python code will be:

 

An Error!
I will close the program.

 

python-try-and-except-ipcisco-2

 


 

Else Statement

 

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.

 

try:
  a=5
  print(a)
except:
  print("An Error!")
else:
  print(“All is Good!”)

 

The return of this code will be like below:

 

5
All is Good!

 

python-try-except-else-statement-ipcisco-3

 

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!”.

 


 

Raise Statement

 

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.

 

for x in range(10):
               if x > 5:
                raise Exception("Higher 5!")

 

The output will be:

 

 Traceback (most recent call last):
File "./prog.py", line 3, in <module>
Exception: Higher 5!

 

python-raise-statement-www.ipcisco-4

 

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.

 

Back to: Python Programming Course > Python Functions

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course

Collapse
Expand
Latest Blog Posts