467,081 Members | 1,074 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Custom exceptions -- inherit from exceptions.Exception?

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!
Jul 18 '05 #1
  • viewed: 7084
Share:
3 Replies
pw*******@adelphia.net (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
Jul 18 '05 #2
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/


Jul 18 '05 #3
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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Dave | last post: by
11 posts views Thread by C# Learner | last post: by
9 posts views Thread by Jeff Louie | last post: by
5 posts views Thread by Bevo | last post: by
2 posts views Thread by Rajeev Soni | last post: by
15 posts views Thread by bill salkin | last post: by
6 posts views Thread by Lance | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.