Custom exceptions -- inherit from exceptions.Exception?

Discussion in 'Python' started by Paul Miller, Nov 12, 2003.

  1. Paul Miller

    Paul Miller Guest

    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!
    Paul Miller, Nov 12, 2003
    #1
    1. Advertising

  2. (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
    Martin v. =?iso-8859-15?q?L=F6wis?=, Nov 12, 2003
    #2
    1. Advertising

  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).
    >
    >

    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/
    Mike C. Fletcher, Nov 12, 2003
    #3
  4. 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
    Alex Martelli, Nov 12, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LeAnne

    inherit from my custom page

    LeAnne, Aug 26, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    970
    LeAnne
    Aug 26, 2006
  2. __PPS__
    Replies:
    3
    Views:
    1,507
    Alf P. Steinbach
    Nov 9, 2005
  3. John Smith
    Replies:
    5
    Views:
    704
    John Smith
    Apr 27, 2011
  4. Andrew

    Custom Control - inherit child properties?

    Andrew, Jul 16, 2003, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    181
    Andrew
    Jul 17, 2003
  5. Joey Lee

    Setting values for custom control inherit from DataGrid

    Joey Lee, Mar 31, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    140
    Joey Lee
    Mar 31, 2005
Loading...

Share This Page