<p dir="ltr"><br>
On 5 May 2016 9:16 am, "Kyle Lahnakoski" <<a href="mailto:klahnakoski@mozilla.com">klahnakoski@mozilla.com</a>> wrote:<br>
><br>
><br>
> On 5/4/2016 7:05 PM, <a href="mailto:cs@zip.com.au">cs@zip.com.au</a> wrote:<br>
> > I also like context from close to where the exception occurred, while<br>
> > doing that catching at whatever the suitable outer layer may be, as<br>
> > normal. Let me show you what I do...<br>
> ><br>
> > I have a module "cs.logutils":<br>
> ><br>
><br>
> I really like the idea of using a `with` clause for simplify exception<br>
> chaining. I am concerned I would be missing some of the locals<br>
> available at exception time, which the `with` clause would not have<br>
> access to, but more introspection may solve that too. It does not solve<br>
> the indent problem, but I could live with that if it made the code<br>
> simpler in other ways.<br>
><br>
> Many of my exception handlers are multiline, and I do not think the<br>
> `with` clause strategy would work there.</p>
<p dir="ltr">In combination with contextlib.contextmanager (and perhaps passing in references to relevant locals), with statements are designed to handle factoring out almost arbitrary exception handling. Chaining a consistent error, for example:</p>
<p dir="ltr">@contextmanager<br>
def chain_errors(exc_to_raise):<br>
try:<br>
yield<br>
except Exception as e:<br>
raise exc_to_raise from e</p>
<p dir="ltr">This is most useful for supplying state that's useful for debugging purposes (e.g. the codec infrastructure tries to do something like that in order to report the codec name)</p>
<p dir="ltr">Cheers,<br>
Nick.<br></p>
<p dir="ltr">> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a><br>
</p>