[Tutor] capturing exceptions from an exec statement

Garry Willgoose Garry.Willgoose at newcastle.edu.au
Tue Apr 8 09:12:25 CEST 2008


I'm writing a (very simple) command line window for use within my  
Tkinter GUI menu driven program (Aside: I want to provide a Matlab  
like command line capability within an otherwise menu driven  
program ... but where the script language is Python). The guts of  
executing the command are (where commandtext is a text variable  
captured from my command line window)

     try:
       exec(commandtext)
     except:
       print 'failed to execute command'      # we'll do something  
more sophisticated in due course

The question is if commandtext contains invalid python I thought exec  
raised an exception that I could capture. I seem unable to get this  
to throw an exception no matter what is in commandtext. I assume I'm  
missing something simple here.

Alternatively should I open a process with popen ... I want to be  
able to modify variables in the existing scope and the way I read  
popen I can't do that.


====================================================================
Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308
Australia.

Centre webpage: www.c2im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574  
(Fri PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal  
and Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgoose at newcastle.edu.au;  
g.willgoose at telluricresearch.com
email-for-life: garry.willgoose at alum.mit.edu
personal webpage: www.telluricresearch.com/garry
====================================================================
"Do not go where the path may lead, go instead where there is no path  
and leave a trail"
                           Ralph Waldo Emerson
====================================================================







More information about the Tutor mailing list