git @ Cat's Eye Technologies The-Dipple / master python / exception_chaining.py
master

Tree @master (Download .tar.gz)

exception_chaining.py @masterraw · history · blame

#!/usr/bin/env python

# SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense

# It would be nice if there was a standard way to "chain" these
# exceptions together, so that the IndexError's traceback was
# available from inside the KeyError, and printed on exit.
# You can probably do it, but not in an elegant way.

def a(foo):
    try:
        b(foo)
    except Exception, e:
        # raise
        # raise KeyError(e.args[0])
        raise KeyError(e)


def b(foo):
    c(foo)


def c(foo):
    raise IndexError(foo)


a(17)