What's the best way to wrap a whole script in try..except?
Bruno Desthuilliers
onurb at xiludom.gro
Wed Jun 21 06:34:03 EDT 2006
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 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list