Catching user defined exceptions from foreign modules

Derek Basch dbasch at yahoo.com
Mon Sep 30 17:26:28 EDT 2002


Thanks for this snippit:
> For class exceptions, in a try statement with an
> except clause that 
> mentions a particular class, that clause also
> handles any exception 
> classes derived from that class (but not exception
> classes from which 
> it is derived). Two exception classes that are not
> related via 
> subclassing are never equivalent, even if they have
> the same name. 

It really helped me understand. I subclassed my
'TestError' exception from a base exception class
'Error' which was derived from the 'Exception' class.
As shown in this example:
http://www.python.org/doc/current/tut/node10.html#SECTION0010500000000000000000
So I could have just caught the imported modules base
exception class and as long as the generated exception
is a subclass of the base class it will be caught.
Cool!! To bad the python book I learned from never
covered this :(. I will have to add this to a bunch
more modules now that I understand how to use it
properly. I think I had better give the base exception
classes unique names as well to avoid import
conflicts.

Thanks for all the help,
Derek Basch

> > Thanks,
> > By 'built in' I meant the python built in module
> exceptions:
> > http://web.pydoc.org/2.2/exceptions.html
> 
> 	I'll have to confess that I haven't kept fully up
> on the changes (like 
> making all exceptions "class-based").
> 
> 	To me, "built in" tends to imply something
> available without explicit 
> imports...
> 
> 	I think the following paragraph from the document
> is applicable...
> 
> For class exceptions, in a try statement with an
> except clause that 
> mentions a particular class, that clause also
> handles any exception 
> classes derived from that class (but not exception
> classes from which 
> it is derived). Two exception classes that are not
> related via 
> subclassing are never equivalent, even if they have
> the same name. 
> 
> 	You created a subclass exception as I recall (I
> deleted the files I'd 
> copied so can't check). The main program could have
> caught that if you 
> had just listed the base exception rather than the
> specific subclass. 
> Without actually testing code (I'm rushing -- have
> an appointment in 45 
> mins)...
> 
> #t1.py
> from t2 import I_Fail
> 
> try:
> 	I_Fail(123)
> except ArithmeticError, e:
> 	print "Arithmetic Error: '%s' Occurred" % e.value
> 
> 
> #t2.py
> 
> class MyFailure(ArithmeticError):
> 	def __init__(self, value):
> 		self.value=value
> 	def __str__(self):
> 		return `self.value`
> 
> def I_Fail(a):
> 	if a is not None:
> 		raise MyFailure("I_Fail received a non-null
> argument")
> 
> 
> 
> >
> > Im assuming these are automatically imported
> anytime the interpreter
> > is fired up. I will use the 'from module import
> alpha, beta,
> > charlie' syntax as this seems to be the best way.
> At least according
> > to the ActivePython documentation/Python HOW
> TO's/Do's and Dont's.
> > They even have your open() idiom in the HOW TO.
> > Thanks for all the help,
> > Derek Basch
> >
> > --- In python-list at y..., Dennis Lee Bieber
> <wlfraed at i...> wrote:
> > > Derek Basch fed this fish to the penguins on
> Monday 30 September
> >
> > 2002
> >
> > > 08:59 am:
> > > > to have to import user defined exceptions? I
> dont
> > > > rememeber having to import any of the built in
> > > > exceptions. So, here is the output of the
> fixed test1:
> > >
> > >         "built-in" -> already there... <G>
> > >
> > > > test2.TestError
> > > > fudge
> > > >
> > > > Am I doing this all wrong?
> > >
> > >         You want to use items defined in an
> imported module. I see
> >
> > three
> >
> > > alternatives...
> > >
> > > import module
> > >
> > >         all uses then become module.item
> > >
> > > from module import item1, item2, ... itemN
> > >
> > >         only the named items are useable, but
> can be used as itemM
> > >
> > > from module import *
> > >
> > >         practically everything within the module
> is available as
> >
> > item
> >
> > > The last is not recommended as you can encounter
> unexpected
> > > redefinition of names... In the last week we had
> one such...
> > >
> > > from os import *
> > >
> > > replaces the Python open() (which I understand
> is being phased out
> >
> > for
> >
> > > file()) with the low-level (C-style) open --
> totally different
> > > arguments needed.
> > >
> > > --
> > >
> > >  >
>
==============================================================
> <
> > >  >   wlfraed at i...  | Wulfraed  Dennis Lee Bieber
>  KD6MOG <
> > >  >      wulfraed at d...     |       Bestiaria
> Support Staff       <
> > >  >
>
==============================================================
> <
> > >  >        Bestiaria Home Page:
> http://www.beastie.dm.net/         <
> > >  >            Home Page:
> http://www.dm.net/~wulfraed/             <
> > >
> > > --
> > >
> http://mail.python.org/mailman/listinfo/python-list
> 
> -- 
> --
>  >
>
==============================================================
> <
>  >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee
> Bieber  KD6MOG <
>  >      wulfraed at dm.net     |       Bestiaria
> Support Staff       <
>  >
>
==============================================================
> <
>  >        Bestiaria Home Page:
> http://www.beastie.dm.net/         <
>  >            Home Page:
> http://www.dm.net/~wulfraed/             <


__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com




More information about the Python-list mailing list