1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Putting It All Together With Syntax

From Al Lukaszewski, for About.com

8 of 10

Exceptions and Errors

In writing a Python program, you should always include some form of error checking. Doing so allows the computer to fail softly instead of giving the user an ugly error message. It also means that if one thing goes wrong the entire program does not grind to a halt.

To provide this safety net, use 'try...except'. 'try' tells the computer to attempt to do something. 'except' tells it what to do on failure. The syntax goes like this:

try:
a = open("sample.txt", "r")
except IOError, error:
print error
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.