Exception handling

Michael Hudson mwh at python.net
Wed May 8 07:37:28 EDT 2002


Philip Swartzleonard <starx at pacbell.net> writes:

> Michael Hudson || Wed 08 May 2002 03:58:31a:
> 
> > Järvinen Petri <jarvin24 at lehtori.cc.tut.fi> writes:
> > 
> >> Hello
> >> 
> >> Is it possible to use dynamic binding in exception handling like in
> >> C++. 
> >> 
> >> I mean can I catch baseclass exception in my exception handler and
> >> get all the derived classes catched aswell, like
> > 
> > Yes.  Didn't you try it?  Although A should have Exception amongst
> > it's base classes...
> 
> Should it? 

Yes, but it's only "should".  It's not enforced anywhere.  There was
talk of making it enforced, but I don't think that got anywhere.

> I've been meaning to ask, but I haven't been pythoning much 
> lately so it hasn't come up again. When defining your own exception 
> heirarcy, should you derive from the standard exception class or just 
> start from scratch, and why?

Derive from the standard one.  Why?  Erm, because You Should.  There
are probably better reasons, but I can't be bothered to think them up
right now.  Alex probably will.

> Also, what about raising things like strings as exceptions (which
> i've done in exapmle classes for CIS/DS projects, e.g. 'Raise
> "Linked list is empty, please verify with is_valid"' or whatever
> just for documention really).

That's very backward behaviour; string exceptions fell out of favour
with Python 1.5 (I think; might have been 1.4 or even earlier).  You
can still do this, but you shouldn't in new code.

Not sure what "things like strings" means; try raising anything other
than a string or an (old-style!) instance and you'll get a TypeError
instead.

Cheers,
M.

-- 
  US elections
  For those of you fearing that the rest of the world might be 
  making fun of the US because of this: Rest assured, we are.
         -- http://www.advogato.org/person/jameson/diary.html?start=12



More information about the Python-list mailing list