Custom exceptions -- inherit from exceptions.Exception?

P

Paul Miller

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!
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

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
 
M

Mike C. Fletcher

Paul said:
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/
 
A

Alex Martelli

Paul said:
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:
.... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top