[Tutor] Trapping exceptions in batch scripts

Charlie Clark Charlie@begeistert.org
Sun, 06 Jan 2002 17:25:53 +0100


Dear all,

I have a collection of scripts (about) which are scheduled to run 
repeatedly. As there are occasionally errors I need to catch the 
exceptions and have decided to write an external script which simply 
runs other scripts and logs any errors. I thought this would be better 
than trying to implement error handling in each script but would 
appreciate comments on this.

script_runner.py takes the last command line argument as the name of the 
script to be run.

There seems to be two ways of actually running a script either by 
importing it or by using execfile
Import has to be called via __import__ as it is not possible to pass a 
variable to import. This seems somewhat clumsy but when I tried execfile 
I got some confusing error messages claiming that names weren't properly 
defined. The scripts themselves ran fine but occasionally produced name 
errors when run using execfile.

I've read that execfile is very similar to __import__ except that is 
doesn't create any modules. I'm not sure what this means but I can 
imagine it has something to do with namespaces. Would be grateful for an 
explanation.

Charlie