Unusual Exception Behaviour

mk mrkafk at gmail.com
Thu Jul 17 12:28:02 EDT 2008


Robert Rawlins wrote:

> I certainly like that implementation for logging the exceptions, however, at
> the moment I don't even know where the exceptions are occurring, or what
> type they are, could I still use this method to log any and all exceptions
> raised in the application? 

Sure.

I'm a little confused as to how I can modify that
> implementation to do so.

Remember, Google is your friend, here's the crux of the method without 
the fancy schmancy sugar coating:

http://linux.byexamples.com/archives/365/python-convey-the-exception-traceback-into-log-file/

if __name__=="__main__":
     try:
         main()
     except:
         print "Trigger Exception, traceback info forward to log file."
         traceback.print_exc(file=open("errlog.txt","a"))
         sys.exit(1)


This will obviously capture every exception happening in the main() 
function.

The drawback of this method is that you cannot capture the  error 
message / object accompanying the exception, as you normally can do 
while capturing specific exception:

 >>> import sys
 >>> import traceback

 >>> def anindextoofar(alist):
	print alist[10]

	
 >>> shortlist=range(1,10)
 >>>
 >>> try:
	anindextoofar(shortlist)
except IndexError, err:
	print err
	traceback.print_exc(file=sys.stdout)

	
list index out of range
Traceback (most recent call last):
   File "<pyshell#177>", line 2, in <module>
   File "<pyshell#169>", line 2, in anindextoofar
IndexError: list index out of range







More information about the Python-list mailing list