1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Putting It All Together With Syntax

From , former About.com Guide

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

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Python

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

All rights reserved.