Which exception has been used? (was: Which exception to use?)

Chad Netzer cnetzer at mail.arc.nasa.gov
Thu Jan 30 21:09:37 EST 2003


On Thu, 2003-01-30 at 16:10, Gonçalo Rodrigues wrote:

> Does this solve your problem?
> 
> >>> try:
> ... 	raise TypeError("This is a whacky example!")
> ... except Exception, e:
> ... 	print e
> ... 	
> This is a whacky example!

Well, I think Cameron's point is that one can have exceptions that are
NOT derived from Exception (although it is very bad style; but you may
not have to be prepared to handle other people's code).  Also, string
exceptions are still (and at least pychecker wil report that as a
warning)

This is an example of the kind of problem one might have to be on the
lookout for:


output:

caught MyException
caught Exception
caught non-standard exception __main__.MyDumbException
<__main__.MyDumbException instance at 0x815d2c4>
caught non-standard exception eggs None


code:
"""

import sys
 
class MyException( Exception ):
    pass
 
class MyDumbException:
    pass
 
for exception in (MyException, Exception, MyDumbException, "eggs"):
    try:
        raise exception
    except MyException, e:
        print 'caught MyException', e
    except Exception, e:
        print 'caught Exception', e
    except:
        name, e, traceback = sys.exc_info()
        print 'caught non-standard exception', str( name ), e
"""
-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list