5 Dealing with Errors

One of the major advantages to writing scripts in Python rather than in the IRAF CL is the ability to handle errors using try and except. This works even when calling IRAF tasks; that is, an error in a CL script or a call error in an SPP program can be caught using try in Python. For example,

--> try:
...   iraf.columns ("garbage", 37)
... except iraf.IrafError, e:
...   print "error was caught"
...   print e
Killing IRAF task `columns'
error was caught
Error running IRAF task columns
IRAF task terminated abnormally
ERROR (741, "Cannot open file (garbage)")

There's a catch, however. Many IRAF tasks can operate on a list of input files, and these tasks usually convert some errors (such as file not found) into a warning, allowing the task to continue trying to process the other input files. But warnings are not caught by try and except. The imstat task used by nc.py in the ncounts example is a case in point; a try statement was not used there because it would not trap the most common problems, such as a mistake in the name of the input image. If you want to do robust error handling, it is best to check parameter values in Python before calling the IRAF task to ensure predictable behavior when errors occur.

Questions or comments? Contact help@stsci.edu
Documented updated on