[Issues] errors in python-codes

John P. McFarland mcfarland at astro.rug.nl
Sun Feb 5 21:22:56 CET 2006


Teffie,

Typically, exceptions are raised when errors occur.  The common way to 
handle this is using a try/except clause: a set of statements are tried, 
and any exception returned following the commands given in the except part 
of the clause.  For example:

try:
    filename = 'example.dat'
    fd = file(filename, 'r')
except TaskError:
    print 'Could not open file %s' % filename

This code attempts to open, readonly, the file 'example.dat'.  If that
fails, an exception is raised, printing the error and typically stopping
the execution of the recipe (i.e., if class TaskError is of type
'Exception').

In Astro-WISE code, these statements are within classes and recipes, and
stop execution so that the user can see what happened and where it
happened so it can be fixed.  This unfortunately stops any automated
calling script, even in the middle of a loop.  The simplest way to keep
automated scripts going despite this is to use a simpler try/except
clause from within the calling script:

while no_problems:
    try:
        lines of code to be run multiple times
    except:
        pass

In this case, any exceptions will be ignored by the calling script (pass
statement), and the loop can continue uninterrupted.  

Cheers,


-=John



On Fri, 3 Feb 2006, Teffie Schneider wrote:

> When making automized scripts to do certain tasks several times in a row
> (for different chips, filters etc..) it would be nice if errors were
> 'raised' in such a way that the script doesn't quit hereafter but
> 'continues' with the next file... automized scripts are very handy when
> reducing data so I think this wouldn't be any luxury or something... ;-)
> 



More information about the Issues mailing list