source: sasmodels/sasmodels/exception.py @ 9e430d0

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 9e430d0 was 9e430d0, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

move exception annotation into its own module

  • Property mode set to 100644
File size: 828 bytes
Line 
1"""
2Utility to add annotations to python exceptions.
3"""
4
5def annotate_exception(exc, msg):
6    """
7    Add an annotation to the current exception, which can then be forwarded
8    to the caller using a bare "raise" statement to reraise the annotated
9    exception.
10    Example::
11        >>> D = {}
12        >>> try:
13        ...    print D['hello']
14        ... except Exception,exc:
15        ...    annotate_exception(exc, "while accessing 'D'")
16        ...    raise
17        Traceback (most recent call last):
18            ...
19        KeyError: "hello while accessing 'D'"
20    """
21    args = exc.args
22    if not args:
23        exc.args = (msg,)
24    else:
25        try:
26            arg0 = " ".join((args[0],msg))
27            exc.args = tuple([arg0] + list(args[1:]))
28        except:
29            exc.args = (" ".join((str(exc),msg)),)
30
Note: See TracBrowser for help on using the repository browser.