On Wed, Mar 6, 2013 at 10:32 AM, Peter Waller <[email protected]> wrote:
> I'm not sure _as an experienced python developer_ how to write code which
> guarantees it doesn't raise a KeyError
Inside of your __getitem__ you have total control over what that method
will return. You only want a KeyError to be raised if you are sure that the
goal is to exit with no context. Thus, define what you mean by no context
as "class NoContext(Exception): pass" .
def __getitem__(self, key):
try:
# stuff to find context
return context
except NoContext:
raise KeyError
except KeyError:
raise SomeOtherExceptionThatIsNotKeyError
There. Any KeyError raised within the code will be properly handled as a
raw exception, and your contract with any code within __getitem__ that
would be responsible for returning an invalid context should raise
NoContext. Obviously this may not be ideal, but it's not like the framework
is preventing you from having full control over what your code is doing.
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.