how to enable error message in exec??

Alex Martelli aleaxit at yahoo.com
Mon Apr 9 03:55:58 EDT 2001


"Quinn Dunkan" <quinn at retch.ugcs.caltech.edu> wrote in message
news:slrn9d1r0h.rbg.quinn at retch.ugcs.caltech.edu...
    [snip]
> try:
>     exec eventHandler[0]
> execpt:
>     exc = sys.exc_info()[0]
>     print exc
>     # ...

Right!  If it's important to you to catch syntax errors in
the string separately from those its _execution_ itself may
raise, you can also use built-in function compile first (it
compiles the string into a code-object, raising syntax errors
if need be), then use the code-object as exec's argument.


> I can't say if 'exec' is a good solution here, but it's probably not :)
If

Right again!  If nothing else, because exec is RARELY the right
solution -- it's more of a "quick-fix that I will refactor soon"
kind of thing (most of the time:-)...


Alex






More information about the Python-list mailing list