[New-bugs-announce] [issue6210] Exception Chaining missing method for suppressing context

Patrick W. report at bugs.python.org
Fri Jun 5 21:04:25 CEST 2009

New submission from Patrick W. <poke at BornToLaugh.de>:

I'm currently writing a library that executes predefined http requests
to a specified server. In case there is for example a HTTP Error, I want
to raise a user-defined exception that contains additional information
about when the error actually occured (so that the user of that library
gets information what happened without having to look at the actual
library code).
The in Python 3 introduced "feature" of chained exceptions (PEP 3134) is
something which is a bit annoying in that particular situation. While it
is probably good in a lot situation to know where an exception comes
from (especially when they are chained), it makes reading the actual
exception for the library user harder.
Let me show you an example:

def doXY ():
  # ...
    page = urlopen( someRequest )
  except urllib.error.URLError as e:
    raise MyError( 'while doing XY', e )
  # ...

MyError is an exception class, that uses the second parameter to get
additional information (for HTTPErrors the status code for example) and
compiles a detailed error message.
Before Python 3, this was a good way to prevent users from having to dig
into the code when they encounter an exception. Now however, you get
some error message like this:

Traceback (most recent call last):
  File "..", line ., in doXY
    page = urlopen( someRequest )
  File "..\lib\urllib\request.py", line 122,
in urlopen
    return _opener.open(url, data, timeout)
  File "..\lib\urllib\request.py", line 364,
in open
    response = meth(req, response)
  File "..\lib\urllib\request.py", line 476,
in http_response
    'http', request, response, code, msg, hdrs)
  File "..\lib\urllib\request.py", line 402,
in error
    return self._call_chain(*args)
  File "..\lib\urllib\request.py", line 336,
in _call_chain
    result = func(*args)
  File "..\lib\urllib\request.py", line 484,
in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "..", line ., in <module>
  File "..", line ., in doXY
    raise MyError( 'while doing XY', e )
MyError: 'HTTP Error 401 while doing XY (Unauthorized)'

While the error message of MyError would be completely sufficient for
the user to know (and to fix by simply giving correct data) he is
flooded with a lot of information about the previously raised exception
instead with far too many unneccessary information.

So what I basically would like to have is some mechanism to prevent
Python from printing out all previous exception. As there is already the
'raise NewException from PreviousException' syntax, something like
'raise NewException from None' would be great, with explicitely stating
None to clear the buffer of previous exceptions.

Thanks you for any comments on this issue.
Patrick W.

components: Interpreter Core
messages: 88965
nosy: poke
severity: normal
status: open
title: Exception Chaining missing method for suppressing context
type: feature request
versions: Python 3.0

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list