Exceptions - built-in and home-grown

Dale Strickland-Clark dale at riverhall.NOSPAMco.uk
Mon Sep 17 15:24:36 EDT 2001


Heiko Wundram <heikowu at ceosg.de> wrote:

>On Monday 17 September 2001 18:42, you wrote:
>> Where can I find a list of all the built-in Python exceptions?
>
>Look at the Python Library Reference (chapter 2.2, I think..., somewhere at 
>the beginning).
>
>> Why would I want to define my own exceptions if suitable Python ones
>> exist?
>
>You should want to define your own exceptions because Python does not define 
>suitable exceptions for returning a certain error condition in your program.
>
>In other words, the answer to your question above is: don't. :)
>
>> Do people do processing in the init routine in their own exceptions?
>
>Yep. I've attached an example of an Exception virtual base class. It is 
>derived directly from Python's Base Exception class and uses some __init__ 
>magic to log the exceptions to a file, if the user desires this. A derived 
>exception only has to overwrite Create() and errormsg(), the rest is done in 
>the base object.
>
>Btw: I've put the import statements for the two extra classes that are 
>referenced at the back, because otherwise a circular import would take 
>place... :)
>
>If you're nterested in the whole project (CGIgen), look for it on 
>SourceForge.net.
>
>> Thanks for any insight.
>
>Hope this helps!

Thanks to both for the replies.

The problem with ANY sort of processing in the exception init routine
is that it will only process your own exceptions. If you want to catch
all exceptions you will need to apply careful use of try/except - in
which case you may as well do all the exception handling there.

Or have I missed something important?
--
Dale Strickland-Clark
Riverhall Systems Ltd



More information about the Python-list mailing list