syntax checking underway; question about catching exceptions

Preston Landers prestonlanders at my-deja.com
Thu Dec 2 12:57:32 EST 1999


Very helpful, Fredrik.  Thanks alot!!!  I don't know what I would do
without the Python newsgroup and website.  Erm, probably start hacking
in Perl full-time. And we don't want that, do we? ;-)

----Preston

In article <00ce01bf3c5e$2d1645d0$f29b12c2 at secret.pythonware.com>,
  "Fredrik Lundh" <fredrik at pythonware.com> wrote:

> oh, you're close.  the exception instance (msg
> in your case) contains the information you're
> looking for.  consider this little example:
>
>     import sys, traceback
>
>     try:
>         compile("""\
>     while 1:
>         prnt 'foo'
>     """, "", "exec")
>     except SyntaxError:
>         ev = sys.exc_info()[1]
>         for k, v in vars(ev).items():
>             print k, "=", repr(v)
>
> which prints:
>
>     filename = None
>     lineno = 2
>     args = ('invalid syntax', (None, 2, 14, "    prnt 'foo'\012"))
>     offset = 14
>     text = "    prnt 'foo'\012"
>     msg = 'invalid syntax'
>
> also see:
> http://www.python.org/doc/current/lib/module-exceptions.html
>
> hope this helps!
>
> </F>
>
>

--
|| Preston Landers <prestonlanders at my-deja.com> ||


Sent via Deja.com http://www.deja.com/
Before you buy.




More information about the Python-list mailing list