On 21/06/06, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
Hari Sekhon wrote:
> I want to wrap a whole script in try ... except. What is the best way of
> doing this?
>
> Consider the following: -
>
> try:
> import <modules>
>
> <CODE>
>
> def notifyme(traceback):
> code to tell me there is a problem
>
> except Exception, traceback:
> notifyme(traceback)
>
>
> Would this code not work because if any part of <CODE> encounters an
> exception then it won't reach the notifyme() function definition and
> therefore the whole thing won't work and I won't get notified when a
> traceback occurs, in fact the call to notifyme() under except will
> itself probably trace back as well!
Yes.
> Do I have to instead do:
>
> import <a couple of modules>
> def notifyme():
> code to tell me there is a problem
>
> try:
> <CODE>
>
> except Exception, traceback:
> notifyme(traceback)
>
Would work, but...
> How you you handle this?
I don't put the main logic at the top level - I use a main() function.
import <some modules>
def notifyme(e):
# code here...
def main(*args):
try:
# code here
return 0
except Exception, e:
notifyme(e)
return <some-non-zero-error-code>
if __name__ == '__main__':
import sys
sys.exit(main(*sys.argv))
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in ' [EMAIL PROTECTED]'.split('@')])"
--
http://mail.python.org/mailman/listinfo/python-list
Why bother passing the args in def main(*args): and main(* sys.argv)? Couldn't you just use sys.argv straight off inside main, no passing args around....
I can see you are probably a C programmer as well?
-h
-- http://mail.python.org/mailman/listinfo/python-list