Reading Until EOF

Donn Cave donn at drizzle.com
Wed Oct 22 01:52:24 EDT 2003


Quoth Kirk Strauser <kirk at strauser.com>:
| At 2003-10-21T18:27:57Z, "Scott Brady Drummonds" <scott.b.drummonds.nospam@=
| intel.com> writes:
|
|> Per my experience with C++, this does not qualify as "exceptional
|> behavior".  Frankly, it is accepted.  I'd like to save the exceptions for
|> true exceptions.  My next guess:
|
| But it *is* exceptional behavior for the file read to fail.  The normal
| behavior is for the call to succeed, right?

Indeed, let me be the 7th or so person to chime in here.  To be
precise, I think rather than `normal' and `succeed', I would just
say that the function has a meaning like `next line from file'.
Functions with such simple meanings can exist because of exceptions -
we decide what the function means, and we make it truly mean that by
raising an exception otherwise.

| Exceptions are handy for all sorts of things.  One of my projects uses
| something like:
|
|     class Success(Exception):
|         def __init__(self, added = 0, deleted = 0, modified = 0):
|             self.added = added
|             self.deleted = deleted
|             self.modified = modified
|
| to hold return values from various calls.  I use it like:
|
|     try:
|         SomeDatabaseQuery()
|     except Success, result:
|         print 'Just added %d new entries.' % result.added
|     except:
|         print 'The query failed.'

Hm, it isn't so clear why this is a good idea.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list