This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients draghuram, ncoghlan, poke, steven.daprano
Date 2010-12-04.01:45:41
SpamBayes Score 1.00332e-08
Marked as misclassified No
Message-id <>
It seems to me that an explicit raise inside an except block should *not* chain exceptions by default. The rationale for chaining exceptions is to detect bugs in the exception handler:

except SomeError:
    y = 1/x  # oops, what happens when x == 0?

but an explicit raise isn't a bug. If you want it to chain for some reason, it's easy to do: 

except SomeError as exp:
    raise MyException from exp

or otherwise set __content__, but there's no simple way to avoid chaining except via boilerplate code.

I frequently use the EAFP idiom to detect error conditions, and chained exceptions exposes details to the caller that are irrelevant implementation details. Here's a common example:

def process(iterable):
        x = next(iterable)
    except StopIteration:
        raise ValueError("can't process empty iterable")

The fact that ValueError was raised in response to a StopIteration is an irrelevant implementation detail that shouldn't be, and wasn't in 2.x, exposed.

Another practical example is avoiding isinstance checks:

def func(x):
        x + 0
    except (ValueError, TypeError):
        raise MyException('x is not a number')

I note that PEP 3134 explicitly listed the issue of *not* chaining exceptions as an open issue. Now that chained exceptions are being seen in the wild, it seems that the inability to easily suppress chaining *is* a real issue for some users:
Date User Action Args
2010-12-04 01:45:45steven.dapranosetrecipients: + steven.daprano, ncoghlan, draghuram, poke
2010-12-04 01:45:45steven.dapranosetmessageid: <>
2010-12-04 01:45:41steven.dapranolinkissue6210 messages
2010-12-04 01:45:41steven.dapranocreate