[Python-ideas] Improving Catching Exceptions

Sven R. Kunze srkunze at mail.de
Tue Jun 27 15:49:15 EDT 2017


On 27.06.2017 13:41, Nick Coghlan wrote:
> The shallow exception notion breaks a fairly fundamental refactoring
> principle in Python: you should be able to replace an arbitrary
> expression with a subfunction or subgenerator that produces the same
> result without any of the surrounding code being able to tell the
> difference.

I would agree with you here but this "refactoring principle in Python" 
doesn't work for control flow.

Just look at "return", "break", "continue" etc. Exceptions are another 
way of handling control flow. So, this doesn't apply here IMO.

> By contrast, Steven's exception_guard recipe just takes the existing
> "raise X from Y" feature, and makes it available as a context manager
> and function decorator.

I don't see how this helps differentiating shallow and nested exceptions 
such as:

try:
     with exception_guard(ImportError):
         import myspeciallib
except RuntimeError: # catches shallow and nested ones
     import fallbacks.MySpecialLib as myspeciallib


Regards,
Sven

PS: this has nothing to do with cyclic imports. It can be a 
misconfiguration of the system which fails nested imports. In those 
cases, we fallback silently.


More information about the Python-ideas mailing list