[Tutor] Exceptions and wotnot

Andy W toodles@yifan.net
Wed, 20 Feb 2002 20:46:00 +0800


>   But it's not really what I want.  Surely there must be some way of
getting
> the traceback, out of the except Excpeption: construct.  I dir()ed the

The "sys" module has a reference to the last traceback, strangely enough
called "last_traceback" ;)
You might also want to look at the "traceback" module, as it does some work
for you.

Example:

>>> this_is_just_to_create_an_error
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    this_is_just_to_create_an_error
NameError: name 'this_is_just_to_create_an_error' is not defined
>>> import sys,traceback
>>> tb=sys.last_traceback
>>> tb_info=traceback.format_tb(tb)
>>> print tb_info
['  File "C:\\PYTHON22\\Tools\\idle\\PyShell.py", line 274, in runcode\n
exec code in self.locals\n', '  File "<pyshell#1>", line 1, in ?\n
this_is_just_to_create_an_error\n']
>>> print ''.join(tb_info)
  File "C:\PYTHON22\Tools\idle\PyShell.py", line 274, in runcode
    exec code in self.locals
  File "<pyshell#1>", line 1, in ?
    this_is_just_to_create_an_error

>>>

HTH,
Andy

>   Thanks guys,
>   Glen
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>