[Chicago] How to know more about the raised exception?

Martin Maney maney at two14.net
Sun Nov 4 20:44:18 CET 2007


On Sun, Nov 04, 2007 at 01:09:06PM -0500, Cosmin Stejerean wrote:
> You can create a new exception class in which you can wrap other exceptions
> and raise that instead. So your error handling code can easily know if it's
> an "original" exception or an exception you raised.

> On 11/4/07, Timmy <timheit at netvigator.com> wrote:
> >    I know that an exception can be re-raised.
> > Is there any simple way provided by python itself that I can know the
> > current exception is
> > just firstly occurred or it is re-raised by previous exception?

The problem with a wrapper class is that (at least without getting a
good deal more involved) it will break any handlers that catch a subset
of exceptions.  If you can be reasonably sure the exception objects
aren't strings (which are sort of obsolete, though the language
probably still accepts them until 3.0 - anyway, there are non-archaic
verisons of Python that permit them) the obvious way is to simple add
an attribute to the exception the first time it's seen, and test for
that to decide how to handle it.  If you do need to worry about
exceptions that are Just Strings you'll need to do a little more work,
but it's probably a good idea to wrap both the marking and the testing
in a little function anyhow, just to be certain it gets done
consistently.  Probably just give up and wrap strings in your own class
- they're not commonly used with type-based catchers for the obvious
reasons that all strings are the same type.

Luck!

PS: I hope this is a debugging expedient.  It feels like the sort of
thing that should never go into "real" code, but might be real useful
when trying to sort out unexpected behavior.

-- 
Man's mind, once stretched by a new idea,
never regains its original dimensions.  -- Oliver Wendell Holmes



More information about the Chicago mailing list