[Python-Dev] Explicitly declaring expected exceptions for a block

Dmitry Dvoinikov dmitry at targeted.org
Tue Jun 21 08:22:54 CEST 2005


> If you're trying to write tests, perhaps a better use-case would be
> something like:

> with required_exception(SomeError):
>     do something that should cause SomeError

Yes, you are right, that'd be a better and more flexible way,
thank you.

Sincerely,
Dmitry Dvoinikov
http://www.targeted.org/

--- Original message follows ---

> On 6/20/05, Dmitry Dvoinikov <dmitry at targeted.org> wrote:
>> Excuse me if I couldn't find that in the existing PEPs, but
>> wouldn't that be useful to have a construct that explicitly
>> tells that we know an exception of specific type could happen
>> within a block, like:

>> ignore TypeError:
>>     do stuff
>> [else:
>>     do other stuff]

> If I understand PEP 343 correctly, it allows for easy implementation
> of part of your request. It doesn't implement the else: clause, but
> you don't give a use case for it either.

> class ignored_exceptions(object):
>     def __init__(self, *exceptions):
>         self.exceptions = exceptions
>     def __enter__(self):
>         return None
>     def __exit__(self, type, value, traceback):
>         try:
>             raise type, value, traceback
>         except self.exceptions:
>             pass

> with ignored_exceptions(SomeError):
>     do stuff

> I don't see the use, but it's possible.

>> The reason for that being self-tests with lots and lots of
>> little code snippets like this:

> If you're trying to write tests, perhaps a better use-case would be
> something like:

> with required_exception(SomeError):
>     do something that should cause SomeError

> paul



More information about the Python-Dev mailing list