Exception Handling in Python
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.
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.
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.
Like us on Facebook
We need your assistance to stay live
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Install LAMP Server (Apache, MySQL or MariaDB, PHP) On Ubuntu 13.10 : Updated January 12, 2014 LAMP is a combination of operating system and open-source software stack. ...0 comments |
- How to Install uTorrent Client on Ubuntu/Debian/Linux Mint : uTorrent is a lightweight and efficient BitTorrent client for Linux, Windows OS and Mac OS. The inst...0 comments |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- DNS Server Installation Step by Step Using CentOS 6.5/6.4/6.3 : DNS, Domain Name System, translates hostnames or URLs into IP addresses. For example if we type www....0 comments |
- Fedora 21 Scheduled For Release In October
- Install Uget Download Manager In Ubuntu, Fedora, Debian
- Download Elementary OS ‘Luna’ Official Wallpapers
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04
- 5 Android Apps that’s Going to Make Running Your Business Less Stressful
- Puppet agent Install Using Script
- Install Corosync and Pacemaker On CentOS 6.5
- Install Fail2Ban On Ubuntu Server 13.04/13.10
- How To Install Google Chrome in Fedora 20, 19, 18
- How to surf using Tor Browser Bundle
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .