Catching and reraising exceptions properly

This new method of reraising exceptions should retain the original traceback. So if you have pdb or ipdb on, you will receive the reraised exception with a more informative message, but your traceback will actually be where the original error occurred. It will also append the original exceptions and their messages to the new message by default.

See also https://groups.google.com/forum/#!topic/pylearn-dev/UbWz5tQDH5M
In Theano, we add more then 1 args at some place. So this can happen. Do
you have a suggestion?


On Fri, Jul 25, 2014 at 12:49 PM, Bart <notifications@github.com> wrote:

> In pylearn2/utils/exc.py:
>
> > +    >>> except Exception:
> > +    >>>     reraise_as(UnhandledException("Informative message"))
> > +    """
> > +    orig_exc_type, orig_exc_value, orig_exc_traceback = sys.exc_info()
> > +
> > +    if isinstance(new_exc, basestring):
> > +        new_exc = orig_exc_type(new_exc)
> > +
> > +    if hasattr(new_exc, 'args'):
> > +        if len(new_exc.args) > 0:
> > +            new_message = new_exc.args[0]
> > +        else:
> > +            new_message = ""
> > +        new_message += ('\n\nOriginal exception:\n\t' + orig_exc_type.__name__)
> > +        if hasattr(orig_exc_value, 'args') and len(orig_exc_value.args) > 0:
> > +            new_message += ': ' + orig_exc_value.args[0]
>
> Actually, that would be confusing, because it wouldn't be clear in the
> printout which arguments came from the original error and from the reraised
> error. Instead I could just use ", ".join(orig_exc_value.args).
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/lisa-lab/pylearn2/pull/1039/files#r15411327>.
>