Best practice for handling exceptions raised at lower levels?

Antoon Pardon antoon.pardon at vub.be
Tue Feb 2 06:07:12 EST 2021


Op 2/02/21 om 01:54 schreef Skip Montanaro:
> Here's the crux of the problem. Where does responsibility generally
> fall for low level exception handling? I don't mean to pick on
> IMAPClient. It's just a recent example and got me thinking about the
> problem. Is it (generally) the responsibility of the application
> author or the package author? This isn't an issue just for network
> applications with a number of moving parts (socket, ssl, higher level
> packages, etc). It happens in almost all applications or packages,
> even if it's just to deal with exceptions raised by internal data
> structures.

Here is my two cents worth.

I think it should be the resposibility of the package. The package 
should catch the low level exception and raise its own exception. Why? 
Because in a sense we are talking about implementation details. If in 
the future the package would choose an implementation that relies on 
other low-level packages, the appication should still run as before, the 
application shouldn't have to care about how the package is implemented 
and thuse shouldn't have to care about what potential low level 
exception can be raised.

Maybe python should introduce a ComponentError as a new exception. If a 
low level exception is raised in the package, the package should catch 
it and raise the ComponentErrror instead.

-- 
Antoon.


More information about the Python-list mailing list