Forcing exit(); unqualified exception catchers

Fred L. Drake, Jr. fdrake at acm.org
Fri Jan 21 12:06:36 EST 2000


Edward Welbourne writes:
[...lots of stuff about bare except: clauses...]

Eddy,
  An excellent treatise on proper use of exceptions!  I hope all the
new Python users take the time to read through it carefully; it can
really help them create better code.
  Bare except: clauses are also a real problem when debugging, as
well.  Even if you do want to handle an exception that gets raised,
if you don't realize you're getting what you are getting but think its 
something else, your handler will probably break things in subtle ways 
that create a different problem later during execution.  This can be
*very* hard to track down in a highly stateful program!

 > try:
 >     return db.connect(user, password)
 > except (KeyboardInterrupt, SystemExit), what:
 >     raise what

  This can be simplified:

try:
    return db.connect(user, password)
except (KeyboardInterrupt, SystemExit):
    raise

 > So: the real lesson here is, use SystemExit or exit(), not _exit(), and
 > fix the wanton except: clauses that are preventing your SystemExit from
 > having its intended effect.  That way, all your finally: clauses will
 > get to do their stuff.

  There are proper uses of os._exit(), usually related to os.fork()
or running untrusted code that's still active on the stack (i.e., you
do *not* want to hand over control to except or finally clauses).


  -Fred

--
Fred L. Drake, Jr.	  <fdrake at acm.org>
Corporation for National Research Initiatives




More information about the Python-list mailing list