Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Custom exceptions -- inherit from exceptions.Exception?

Reply
Thread Tools

Custom exceptions -- inherit from exceptions.Exception?

 
 
Paul Miller
Guest
Posts: n/a
 
      11-12-2003
Is there any particular good reason to inherit from
exceptions.Exception? I've never seen any code that depends on what
the base class(es) of a raised exception is (are).

My use case is in a game I am writing. The code for my Game class
contains the following:

class Game (object):

def start (self):
try:
self.players[1]
except IndexError:
raise NotEnoughPlayers

where NotEnoughPlayers is really just an empty classic class suitable
for raising. Is there any benefit to importing exceptions and
inheriting from exceptions.Exception, other than maybe theoretical
purity?

Thanks!
 
Reply With Quote
 
 
 
 
Martin v. =?iso-8859-15?q?L=F6wis?=
Guest
Posts: n/a
 
      11-12-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Paul Miller) writes:

> where NotEnoughPlayers is really just an empty classic class suitable
> for raising. Is there any benefit to importing exceptions and
> inheriting from exceptions.Exception, other than maybe theoretical
> purity?


The idea is that, eventually, you can replace

except:

with

except Exception:

with the additional advantage of being able to write

except Exception,e:

Python does not yet enforce exception to inherit from Exception,
but it might some day, at which point you don't have to change
your code.

Regards,
Martin
 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      11-12-2003
Paul Miller wrote:

>Is there any particular good reason to inherit from
>exceptions.Exception? I've never seen any code that depends on what
>the base class(es) of a raised exception is (are).
>
>

I see it all the time:

try:
blah()
except Exception, err: # want to get err object here...
doSomethingToErr( err ) # e.g. log, or add extra data to the
exception instance
raise

Having all exceptions part of the main tree works very nicely for that
kind of thing.
....

>where NotEnoughPlayers is really just an empty classic class suitable
>for raising. Is there any benefit to importing exceptions and
>inheriting from exceptions.Exception, other than maybe theoretical
>purity?
>
>

Just as a note, you only have to do this:

class NotEnoughPlayers( Exception ):
pass

as Exception is in the __builtin__ module.

The "theoretical purity" comes at a fairly low cost, and gives quite a
bit back IMO. Going even a step further and organising the errors you
raise into a reasonable hierarchy, (preferably using standard exceptions
as base-classes) is often likely to pay dividends as well, but that's
another kettle of Cod.

Enjoy,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/




 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      11-12-2003
Paul Miller wrote:

> Is there any particular good reason to inherit from
> exceptions.Exception? I've never seen any code that depends on what
> the base class(es) of a raised exception is (are).
>
> My use case is in a game I am writing. The code for my Game class
> contains the following:
>
> class Game (object):
>
> def start (self):
> try:
> self.players[1]
> except IndexError:
> raise NotEnoughPlayers
>
> where NotEnoughPlayers is really just an empty classic class suitable
> for raising. Is there any benefit to importing exceptions and
> inheriting from exceptions.Exception, other than maybe theoretical
> purity?


Besides the advantage, already pointed out to you, that "except X,x:"
catches all exceptions of any subclass of X (not just of class X itself),
you do get a small but useful amount of "machinery" from class
Exception:

>>> class X(Exception): pass

....
>>> try: raise X(23)

.... except Exception, e: print 'error', e
....
error 23

i.e., Exception subclasses may be instantiated with arguments and
their instances display those arguments when printed.


Alex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inherit logging properties in ja custom jar library? John Smith Java 5 04-27-2011 02:04 PM
inherit from my custom page LeAnne ASP .Net 2 08-26-2006 12:35 PM
Inherit from a custom ListControl Chris Burge ASP .Net Web Controls 1 12-23-2005 08:28 PM
Setting values for custom control inherit from DataGrid Joey Lee ASP .Net Building Controls 0 03-31-2005 06:58 AM
Custom Control - inherit child properties? Andrew ASP .Net Building Controls 6 07-17-2003 02:48 AM



Advertisments