This snippet is useful for about every important program that you'd need to distribute to the public, for when needs to be kept updated (and is potentially full of bugs):
def show_error(caption, fileName):
from tkMessageBox import showwarning # error dialog box
from sys import exc_info, exit # exception info, system exit
from time import ctime # current time and day
error = '\n'.join(str(i) for i in exc_info()) # change all elements of exc_info to str, then join with newline
stream = open(fileName, 'a') # open stream with append
stream.write("%s: %s\n\n"%( ctime(), error )) # append error and time to an error log
stream.close() # close stream
showwarning( caption, error ) # show error
exit() # exit program It is used like so:
def main():
print "This is our main function"
print "Oops, this line is an error: %s"%(x)
try:
main()
except SystemExit: pass
except:
show_error('Program Error', 'errors.log')
And if your program is not using tkinter, then you just replace the 'showwarning' with a call to
win32api.MessageBox(0, message, caption)