Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Veek M Newsgroups: comp.lang.python Subject: Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling Followup-To: comp.lang.python Date: Fri, 14 Nov 2014 12:48:44 +0630 Organization: Home Lines: 38 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit Injection-Date: Fri, 14 Nov 2014 07:18:58 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="f68a2336b1bb3a5b9be9c6035d2d5633"; logging-data="14734"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190URJdr4gffkhifm8j9TcG" User-Agent: KNode/4.4.11 Cancel-Lock: sha1:gz6sZTJxaBQ99ZyPZgWQ81HP9hY= Xref: csiph.com comp.lang.python:80744 In 'Chained Exceptions' - Beazley pg:626 ------------ try: pass except ValueError as e: raise SyntaxError('foo bar') from e ------------- Here, if ValueError is raised and SyntaxError is then raised.. 'e' contains __cause__ which points to the ValueError Traceback. He goes on to say: --------------- A more subtle example of exception chaining involves exceptions raised within another exception handler. For example: def error(msg): print(m) # Note: typo is intentional (m undefined) try: statements except ValueError as e: error("Couldn't parse configuration") ------------------- Here, 'error' generates an inadvertent exception. What i don't understand is this bit: ------------------ For implicit chaining, the _ _context_ _ attribute of an exception instance e contains a reference to previous exception. --------------------- Why is he saying that 'e' contains a reference in __context__ to the exception generated by 'error' ?? Is that correct? Surely, the exception generated by 'error' will refer to 'e'? WTH?? For Implicit errors, there is no 'e'! Additionally, how would you go about using __context__ if the exception is generated implicitly? Book pages: imgur.com/bwpYq8T imgur.com/inZQv5J