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.

try:
    gak = puke + die
except:
    sys.exit("Oy!")

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__
        try:
                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)
        return(report)

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

Hope this helps!

On 6/13/06, Andy Koch <[EMAIL PROTECTED]> 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@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to