You know you get serious with Python programming language when you start to learn about exception handling. But why do we need exception handling?
The best way to understand exception handling in Python and why do we need it is to look at examples and practice all the time. Sometimes, even if your programs’ syntax is right and you are sure everything is ok, when you execute your Python programs, it gives you errors and stops executing. A classic example that explains exception handling very well is division by zero. Open the Python in interactive mode and try to divide a number by zero. What happens? Everything is correct, your syntax is OK and you wondering why is this happening, why does the program print error and stop running? Wait, let me open a terminal and see what kind of error it is.
Figure 1
Figure 2
Figure 3
Hmm, “ZeroDivisionError: integer division or modulo by zero”. If you know basics in math, you know that you can not divide a number by zero and now that you are reading Unixmen’s article you understand that in computing, dividing by zero crashes the program and return an error. Now, how can we make the program continue working, even if the users want to perform that kind of operation? Imagine a situation where you have to build the calculator and you are not allowed to filter user input, what happens if the user want to divide a number by zero? The calculator will stop working and giving errors to the user, that’s what happens.
Imagine another situation where you have to connect to 100 SSH servers and verify if they are alive, but have you ever thought that one server can back you down? You connect to the first server, to the second and they are up, you try to connect to the third server and the script stops working because the third machine which you are trying to connect is not running SSH at all. Your script stops working, you have no information about other 93 machines, your energy, time and money are gone. Your boss is angry with you and he is thinking to fire you or giving you extra work. If you don’t want to be in this bad situation you have to learn how to do exception handling. We will use try in Python, except our error, store it into a variable and then print this variable to inform the user about the error and why does it happen. This way the user is happy and the program continues executing the other piece of its code.
As I mentioned before, the best way to understand exception handling is to see it in practice, so the figure below explains everything and I have commented each line of the script for you to understand. This is a simple example which will help you to understand exception handling without struggle.
Figure 4
As you can see from Figure 4, when a program is executed it does not gives us error because we used exception handling, we linked error to variable e and printed it out to inform the user about the division by 0. We used ‘except Exception,e’, which is part of Python’s syntax to handle the error and link it to variable.