Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80744

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

From Veek M <vek.m1234@gmail.com>
Newsgroups comp.lang.python
Subject Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling
Followup-To comp.lang.python
Date 2014-11-14 12:48 +0630
Organization Home
Message-ID <m44ad2$ece$1@dont-email.me> (permalink)

Followups directed to: comp.lang.python

Show all headers | View raw


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 

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling Veek M <vek.m1234@gmail.com> - 2014-11-14 12:48 +0630
  Re: Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling Veek M <vek.m1234@gmail.com> - 2014-11-14 18:56 +0630

csiph-web