[Tutor] question about try & except

Kent Johnson kent37 at tds.net
Thu Oct 27 11:47:40 CEST 2005


nephish wrote:
> Hey there,
> 	i am writing some (for me) pretty complicated stuff for work that
> really needs to work. 
> 	i have looked at exception handling in the Learning Python book.
> and i am using some try / except statements. 
> 	the problem is, that even though my script does not crash, i dont know
> the exact error. 
> 	is there a parameter that will allow me to use try and except but that
> will also pring out the traceback statements that python usually does to
> the terminal?

If you put traceback.print_exc() in your exception handler it will print out the exception string and the full stack trace. This is very handy when you have unexpected exceptions as it will let you know where the problem occurs.

For example:
try:
  open('foo.txt')
except IOError:
  import traceback
  traceback.print_exc()

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list