[Tutor] capturing exceptions from an exec statement
Andreas Kostyrka
andreas at kostyrka.org
Tue Apr 8 10:18:37 CEST 2008
Python 2.4.5 (#2, Mar 12 2008, 00:15:51)
[GCC 4.2.3 (Debian 4.2.3-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
... exec("raise ValueError")
... except:
... print "raised some exception"
... import sys
... print sys.exc_info()
...
raised some exception
(<class exceptions.ValueError at 0xb7d83b3c>, <exceptions.ValueError instance at 0xb7d6738c>, <traceback object at 0xb7d65aa4>)
>>>
Something like this?
Andreas
Am Dienstag, den 08.04.2008, 17:12 +1000 schrieb Garry Willgoose:
> 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
> ====================================================================
>
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080408/ef8fcb5b/attachment.pgp
More information about the Tutor
mailing list