1. Home
  2. Computing & Technology
  3. Python

Part 4: Handling Errors the Way You Want

From Al Lukaszewski, for About.com

2 of 7

Illustrate the Problem With an Error

To illustrate this problem, let's introduce a spelling error into 'feedlist.dat'. Two of the most common typos in URLs are the dropping of a full stop (period) and the ommission of the third 'w' in 'www'. Take your pick and perform it on the first line of 'feedlist.dat'. The first line of my data file now looks like this:

Spiegel;http://ww.spiegel.de/schlagzeilen/rss/0,5291,676,00.xml
You can see how easy it would be to make a mistake like this and how hard it would be to detect it by sight. Eventually, we shall add an editing feature into the application, but, for now, we must be wary of mistakes in our data file.

Now start your web browser and direct it to the index page of the RSS Reader. Select Der Spiegel's feed. What happened? It looks like absolutely nothing happened [If you are using the CGI version, you probably received an equally unhelpful "Internal Server Error"]. That is a problem that we are going to fix in this tutorial. One of the golden rules of Python, lines 10 and 11 of the "Zen of Python", reads:

Errors should never pass silently.
Unless explicitly silenced.
We know that an error has occurred because we introduced it. Yet, it does not show in the RSS Reader. Why? Think about it, and then click to the next page to find the answer.

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.