Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling

Veek M vek.m1234 at
Fri Nov 14 07:18:44 CET 2014

In 'Chained Exceptions' - Beazley pg:626

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)
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: 

More information about the Python-list mailing list