2

from the standard library for sys module

sys.excepthook(type, value, traceback)

This function prints out a given traceback and exception to sys.stderr.

When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.

Is there any way of catching uncaught exceptions without necessarily immediately exiting thereafter (depending on a decision-making process)?

later
I suppose the answer is no. I suppose the BDFL, in his wisdom, offers this as a last-ditch opportunity to salvage a few crumbs from a collapsing, not-good program before it crumbles into oblivion leaving only a few logs behind.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • 1
    If used at your entry point, this may help: http://stackoverflow.com/questions/4990718/python-about-catching-any-exception – Kenny Ostrom Aug 05 '15 at 20:20

1 Answers1

1

Below except: acts as a catch all for any uncaught exception but this can have unexpected consequences. A better approach would be to write unit tests and proactively prepare for possible exceptions like a kid throwing unicode in a form.

try:
  # do something

except Exception_Type:
  # do something else

except:
  # woops didn't count on that
Obj3ctiv3_C_88
  • 1,478
  • 1
  • 17
  • 29
  • yes, but of course such a mechanism could be used in place of the sys.excepthook mechanism (if it ends with "exit()"). Let me rephrase my question: why bother having an elegant way of catching uncaughts at all? – mike rodent Aug 05 '15 at 20:32
  • @mikerodent You can override sys.excepthook but you normally shouldn't be messing with uncaught exceptions. Uncaught means that everything you've prepared for has failed and your program is no longer stable. This is because god only knows what happened to cause the exception and a catch all means "do this" without knowing the problem. Just a quick for example, a memory leak. You may have not anticipated for it and by using except you've prevented the default behavior (exit program) which could affect the end user. – Obj3ctiv3_C_88 Aug 05 '15 at 20:46
  • thanks! See my added comment... I concur, essentially! – mike rodent Aug 05 '15 at 20:47