[Zope] Errors
Martijn Faassen
faassen@vet.uu.nl
Sat, 4 Dec 1999 01:07:57 +0100
Daniel G. Rusch wrote:
> What is the defintion of (or where can I find one):
> 1. KeyError
> 2. AttributeError
> 3. NameError
These errors are Python errors. Python raises 'exceptions' when something
goes wrong somewhere. Zope catches these exceptions and displays them. For
someone familiar with the Zope sources (or when developing an extension in
Python) this is very useful, as you can see (in debug mode or in the HTML
comments) exactly in what line in what file things went wrong.
In many cases however something in the bowels of Zope may go wrong and such
an error is raised; the exact Python error message is then not too
informative for the end user, unfortunately. That said, an explanation:
> 1. KeyError
Some code tried to access a dictionary with a key that wasn't in the
dictionary. A dictionary is a kind of table/index. For instance, you could
have a dictionary in Python like this:
dict = {
"Martijn" : "Martijn Faassen",
"The Other Martijn" : "Martijn Pieters",
"Non-Martijn" : "Daniel Rusch"
}
Then, if the Python code did this:
print dict["Martijn"] # or display on a webpage or whatever
You'd see this:
Martijn Faassen
But if you did this:
print dict["Parrot"] # Parrot is not in the dictionary!
You'd see something like this:
...traceback info...
KeyError: Parrot
as the 'key' into the dictionary does not exist. This may happen if you're
trying to access a REQUEST variable that doesn't exist, for instance, as
REQUEST behaves like a dictionary.
> 2. AttributeError
This happens when Python tries to look inside an object for an attribute
that does not exist. This is analogous with a KeyError. For instance, we
may have an object with id 'foo' in the object database. Now we want to
call method 'manage_doWhatever()' in it, which foo defines:
foo.manage_doWhatever()
Now we may make a typo in our DTML:
foo.manage_doWahtever()
Zope will give an AttributeError in that case.
> 3. NameError
Here Python complains that a name (of a variable, a module, a class or
whatnot) does not exist in the current context, for instance:
def foo():
i = 0
print i + j
'j' does not exist and Python will complain with a NameError.
I hope this helps a bit.
Regards,
Martijn