[Tutor] Exceptions: Logging TB and local variables?

Alan Gauld alan.gauld at btinternet.com
Wed Oct 10 10:40:20 CEST 2007


"Kalle Svensson" <kalle.svensson at gmail.com> wrote

>> but only if the data is in scope. If it is local data within the
>> raising function then bit will be gone by the time you catch
>> the exception.
>
> Well, not entirely. If you look at the sys.exc_info() function there
> is a way to get the backtrace of the currently handled exception.

I knew you could get the backtrace but...

>>>> def a(x):
> ...     l = 0
> ...     print x/l
> ...
>>>> def b(y):
> ...     c = 75
> ...     a(y)
> ...
>>>> try:
> ...     b(3)
> ... except:
> ...     t,v,bt = sys.exc_info()
> ...
>>>> bt.tb_next.tb_frame.f_locals
> {'y': 3, 'c': 75}

...I didn't realize it included local variables(eg c) , I thought it 
only had
the functions and the arguments passed. Interesting, and very useful.

Thanks for that,

Alan G




More information about the Tutor mailing list