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
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 |
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7 : OpenLDAP is a free open source Light Weight Directory Access protocol developed by the OpenLDAP proj...0 comments |
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint : Tor, The Onion Router, is a network of Virtual Tunnels that allows users to communicate securely and...2 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 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 |
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6 : Integria IMS is a fully featured ITIL management system, featuring a ticketing system, inventory/C...0 comments |
- Twelve Vulnerabilities Have Been Fixed In Ubuntu 12.04, Time To Update
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
- Selene Media Encoder: Convert Audio, Video Files To Most Popular Formats
- Linux Kernel 3.12.2 Is Available For Download! Install / Upgrade Instructions
- CentOS 6.5 Has Been Officially Released!
- Format Junkie: Convert Media Files To All Popular Formats
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .