try..except with empty exceptions

Chris Angelico rosuav at
Sun Apr 12 01:21:17 CEST 2015

On Sun, Apr 12, 2015 at 9:08 AM, Cameron Simpson <cs at> wrote:
> Catching all exceptions isn't terribly common, _except_ in service routines
> that wrap "unknown" operations. Classic example from my Asynchron class:
>    def call(self, func, *a, **kw):
>      ''' Have the Asynchron call `func(*a,**kw)` and store its values as
>          self.result.
>          If `func` raises an exception, store it as self.exc_info.
>      '''
>      try:
>        r = func(*a, **kw)
>      except:
>        self.exc_info = sys.exc_info
>      else:
>        self.result = r
> All sorts of things like thread pools and other "worker" functions, and
> run-forever daemons like mail filers that can have arbitrary exceptions
> occur in (partly) third party code eg from config files; you need to catch
> any unknown exception and fail the specific action, but continue the main
> daemon operation.
> And since I use this code in Python 2, and since not all exceptions are
> BaseException subclasses, I need the bare syntax.

Fair enough. Do you know how often you actually catch stuff that
wouldn't be caught by "except BaseException:"? I would guess it's
pretty rare. In fact, you can find out.

       r = func(*a, **kw)
     except BaseException:
       self.exc_info = sys.exc_info
       self.exc_info = sys.exc_info
       self.result = r

You could simply mandate that, from version X.Y of your Asynchron
module onward, old-style classes must not be thrown.

> Also, IMO, a bare "except:" syntax is _far_ more pleasing to the eye than
> "except magic_exception_name_that+gets_everything:". Also, I wish
> "BaseException" were just spelled "Exception", if it has to be used.

> I'm -0.1 on the idea myself. I consider "except:" succinct and evocative,
> and prefer it to "except BaseException:".

What you're looking at here is exactly why it's not spelled that way.
The normal thing to do is NOT to catch absolutely everything, but to
allow KeyboardInterrupt and SystemExit to continue to bubble up.
That's spelled "except Exception". My whole point here is that the
bare except catches too much for normal use, and therefore should
_not_ be pleasing to the eye. The thing you should most often be doing
is "except ValueError" or equivalent; forcing you to put at least
_some_ name into the exception clause emphasizes that being specific
is not the unusual case.


More information about the Python-list mailing list