Python Mystery Theatre -- Episode 1: Exceptions

Jason Trowbridge ratman at nmt.edu
Sun Jul 13 18:33:17 EDT 2003


Okay, without looking at anybody else's answers, using the docs, or
trying out the examples.  This is straight from the head, cold.

Act I
's' is a list, and you can't lookup the resulting indice of the
sequence using the brackets.  A s.index( 'a') would be valid.

As for why the exception isn't caught: There is a missing tuple there.
 The following would work:
except (IndexError, TypeError):

The way it is currently given would only catch IndexError exceptions. 
The exception instance would then be bound to the TypeError name.

Act II
The type of msg is always type instance, because it is a class
instance.  The actual class is held under the msg.__class__ attribute.

Act III
I believe that you need to call Exception.__init__(self) in your
constructor.  When the string representation of the exception is
printed, it use's Exception.__str__() (maybe Exception.__repr__().  I
forget which one the traceback shows).  Since the instance hasn't been
initialized properly, that function can't print out its information.

Act IV
This has to do with string exceptions.  I'm not sure how a specific
string exception can be caught.  In any case, this is part of the
reason to stay away from string exceptions.  Additionally, string
exceptions are scheduled to eventually disappear from the language.

Act V
Without looking at the docs, I'd say that KeyError is derived from the
LookupError class.  Exceptions are supposed to be listed most-specific
to least specific, which translates to child classes before parent
classes here.

Hmm, time to look at the answers other people have given, and find how
badly off I am!

 _  () ()      Jason Trowbridge | "There has been a coup. The Mac
( '  .~.     Generic Programmer |  users are liberating the printers."
 \  = o =                       |   --Schlake
---"`-`-'"---+   ratman at nmt.edu |




More information about the Python-list mailing list