Best practice for handling exceptions raised at lower levels?

Chris Angelico rosuav at gmail.com
Tue Feb 2 15:31:35 EST 2021


On Wed, Feb 3, 2021 at 7:26 AM Dan Stromberg <drsalists at gmail.com> wrote:
>
> On Tue, Feb 2, 2021 at 12:00 PM Barry Scott <barry at barrys-emacs.org> wrote:
>
> > When I write packages I aim to trap the exceptions from the lower levels
> > and convert into a package specific exceptions or document that a low
> > level exception can propagate.
> >
> But how do you know what exceptions could be raised?
>
> I love Python in a big way, but this is one thing Java has on Python -
> knowing what exceptions are relevant.

Not really true. Java's declared exceptions still don't tell you
anything about what's *relevant*; only you as a programmer can figure
that out.

> Python's approach is great as long as an uncaught exception should cause
> termination of the program - and most of my programs fit into this
> category.  But not all of them.

If there's some boundary where termination shouldn't happen (for
instance, a web app, where an uncaught exception should kick a 500
back to the client and then go back for another request, or a REPL
where exceptions should be printed to the console and then you
continue), it's easy enough to add a generic handler.

ChrisA


More information about the Python-list mailing list