try:else: w/o except: - why not?

Peter Hansen peter at engcorp.com
Mon Mar 31 22:03:35 EST 2003


John Roth wrote:
> 
> "Peter Hansen" <peter at engcorp.com> wrote:
> > What you wrote in the first place is exactly the same as this:
> >
> > # some code that may except
> > # but if it didn't except, I want to do this
> >
> > In other words, the "try" in your first block is useless....
> 
> Not quite. Consider a test that's supposed to throw an exception
> in a testing harness like unittest or xUnit. If it throws an exception,
> the test passes, and if it doesn't, I want to put out a diagnostic
> message.

Hmmm... interesting and valid point.  I would suspect that this
is an extremely unusual use case, so not worth considering for 
addition to the core, and in the particular case of unittest,
it's already supported by self.assertRaises()!

> Now, that's a bit too simplistic for my use - I'd like to know that
> it threw the *correct* exception. But that's a use case for the
> facility he's asking for.

Exactly... if this is for testing, it's really inadequate to check
that it threw *an* exception, since it might not be the one that
was intended.  Also, adding the "unnecessary" 'except: pass' is
of course as easy as pie.

Manus, we await your justification of the request with bated breath!
:-)

-Peter




More information about the Python-list mailing list