problems with circular references

Jeff Shannon jeff at ccvcorp.com
Wed Mar 27 18:14:16 EST 2002


Terry Reedy wrote:

> "jimh" <jim at jim.jim> wrote in message
> news:a7tbd4$qc7$1 at web1.cup.hp.com...
> > I am working on python code that consist of quite a few files (about
> 50;
> > each file contains one class).  All errors returned by functions are
> numeric
> > and are defined in the individual files.  There is a
> MessageCatalog.py class
> > which will map these numbers into strings.
>
> I would probably follow the standard Python idiom of using exceptions
> instead of numeric error codes, and look at standard library modules
> for precedents.

I very much agree with Terry.  If you define a series of exceptions in a
separate file, then there's no need for mapping error numbers to strings
-- the strings are part of the exception.  This will simplify your code in
many ways, not the least of which is not having to worry about checking
for a numeric error code at *every* level.  Exceptions are *so* much
cleaner, and make it much easier to deal with an error at the most
appropriate level (instead of manually propagating an error condition
through multiple levels of call stack).

-----  MyErr.py ---------

class FooError(Exception):
    pass
---------------------------

------ MyFoo.py ---------
import MyErr
...
try:
    MyObject.Foo():
except MyErr.FooError:
    CancelFoo()
----------------------------

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list