[Tutor] die or exit function?

doug shawhan doug.shawhan at gmail.com
Wed Jun 14 15:17:18 CEST 2006

Hi Andy,

Putting a try:/except: loop in your __main__() (or whatever you call
your base function) with sys.exit("Message") is pretty much the way I
always do it.

    gak = puke + die

If you would like sys.exit() to provide you with a bit more
information (like what actually happened during the failure and
where!) I found this handy function:

def formatExceptionInfo(maxTBlevel=5):
	cla, exc, trbk = sys.exc_info()
	excName = cla.__name__
		excArgs = exc.__dict__["args"]
	except KeyError:
		excArgs = "<no args>"
	excArgsString = ''
	for item in excArgs:
		excArgsString = excArgsString + ' ' + str(item)
	excTb = traceback.format_tb(trbk, maxTBlevel)
	excTbString = ''
	for item in excTb:
		excTbString = excTbString + " " + str(item)
	report = "%s %s %s"%(excName, excArgsString, excTbString)

This function now goes in most of what I do that requires error reporting.

Hope this helps!

On 6/13/06, Andy Koch <andy.koch at pc-doctor.com> wrote:
> Bkgd: I've been doing PHP for the last several years.
> Q: In PHP there are functions die and exit which terminate processing of
> a script with an optional string output.  Is there something similar to
> this in Python?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list