[Tutor] Exceptions: Logging TB and local variables?

Alan Gauld alan.gauld at btinternet.com
Wed Oct 10 09:27:39 CEST 2007


"Allen Fowler" <allen.fowler at yahoo.com> wrote

> Now, I would like to do this: 
> 
> for item in bigset: 
>   try: 
>     self.__sub1(item) 
>     self.__sub2(item) 
>     self.__sub3(item) 
>   except StandardError: 
>     # Log error and continue to next item in set. 
>     log_error_to_file() 
> 
> In the error log, I would like to record a stacktrace and various 
> local variables that existed in subX at the time the Exception 
> was thrown... 

Look at the traceback module.
It was discussed in a recent thread - A Simple question - starting 
on October 2nd...

> (even though the actual exception may have been thrown from 
> deep inside some 3rd party module that subX called) 

Examining data at the time of an exception is easy enough 
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. The only way would be to pass it as part of 
the exception at the point of raise. But if its global or variables 
at the catching level then you can just use them as normal 
from inside the except clause.

HTH,

Alan G



More information about the Tutor mailing list