Name of type of object

Kent Johnson kent37 at tds.net
Thu Feb 10 15:43:23 EST 2005


Jive Dadson wrote:
> I don't think I've quite got it.
> 
> The application I'm writing has some similarities to an interactive
> shell.  Like an interactive shell, it executes arbitrary code that it
> receives from an input stream.  When it gets an exception, it should
> create an informative message, regardless of the type of exception.  The
> traceback routine does that, somehow, some way, but I've tried to read
> that code and figure out how and I don't get it.
> 
> The best I have so far is,
> 
> class Exec_thread(BG_thread):
> 	""" Execute a line of code in the global namespace. """
> 	def _process(s):
> 		""" Process one instruction """
> 		try:
> 			exec s.product in globals()
> 		except (Exception), e:
> 			handle_error( typename(e)+ ": " + str(e) )

Have you looked at the traceback module? If you want to print the same kind of trace you get from 
Python, just use traceback.print_exc().

import traceback
try:
   # whatever
except:
   traceback.print_exc()

Kent

> 
> 
> But that works only if the exception happens to be derived from
> Exception.  How do I handle the
> general case?



More information about the Python-list mailing list