exception problem

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jun 25 03:48:35 EDT 2012


On Sun, 24 Jun 2012 16:16:25 -0700, Charles Hixson wrote:

> But what I wanted was to catch any exception.

Be careful of what you ask for, since you might get it.

"Catch any exception" is almost certainly the wrong thing to do, almost 
always. The one good reason I've seen for a bare except is to wrap the 
top level of an application in order to redirect uncaught exceptions to 
something other than the console (such as a GUI error dialog box). And 
even then, you probably don't want to catch *all* exceptions, but only 
those that inherit from Exception:

try:
    main()  # run my application
except Exception as err:
    display_error_dialog(err)
    # or log to a file, or something...



-- 
Steven



More information about the Python-list mailing list