Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2962

Re: Python program termination and exception catching

References <BANLkTin5ZQFO-t+r=FNEXAnNh9r6r+iW6A@mail.gmail.com>
Date 2011-04-10 19:49 -0400
Subject Re: Python program termination and exception catching
From Jerry Hill <malaclypse2@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.203.1302479386.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Apr 10, 2011 at 3:25 PM, Jason Swails <jason.swails@gmail.com> wrote:
>
> Hello everyone,
>
> This may sound like a bit of a strange desire, but I want to change the way in which a python program quits if an exception is not caught.  The program has many different classes of exceptions (for clarity purposes), and they're raised whenever something goes wrong.  Most I want to be fatal, but others I'd want to catch and deal with.
>
> Is there any way to control Python's default exit strategy when it hits an uncaught exception (for instance, call another function that exits "differently")?

When an exception is raised and uncaught, the interpreter calls
sys.excepthook. You can replace sys.excepthook with your own function.
 See http://docs.python.org/library/sys.html#sys.excepthook

If your program is threaded, you may need to look at this bug:
http://bugs.python.org/issue1230540.  It describes a problem with
replacing sys.excepthook when using the threading module, along with
some workarounds.

There's a simple example of replacing excepthook here:
http://code.activestate.com/recipes/65287/

--
Jerry

Back to comp.lang.python | Previous | Next | Find similar


Thread

Re: Python program termination and exception catching Jerry Hill <malaclypse2@gmail.com> - 2011-04-10 19:49 -0400

csiph-web