472,364 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

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
3 7228
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ken Varn | last post by:
I want to place my own Unhandled Exception Handler application as part of the DbgManagedDebugger registry entry. It is my understanding that you can substitute your own clr unhandled exception...
5
by: Dave | last post by:
Hello all, Unfortunately, the reference I have is a bit slim on describing how to create user-defined exceptions derived from std::exception. I think what I have below will work, but is it the...
11
by: C# Learner | last post by:
What type of exception should I throw when my code detects that a connection has dropped (i.e. NetworkStream.Read() returns 0)? Should I just throw a SocketException or should I create my own...
9
by: Jeff Louie | last post by:
I drank too much coffee last night and came up with a suggestion about how to add checked exceptions to C# http://www.geocities.com/jeff_louie/OOP/oop14.htm Comments expected <g> Regards,...
5
by: Bevo | last post by:
In my enterprise application I'd like my DataAccess layer to be responsible for handling all database error situations and to basically throw custom exceptions according to whether db connection is...
2
by: Rajeev Soni | last post by:
Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if there any exception is occurred in DAL, what is...
2
by: Parag | last post by:
Hi, I am writing error handling mechanism for a large website and one of the requirements that has come is whether it is possible to have the ..NET application whenever it throws exception it...
15
by: bill salkin | last post by:
I'd like to create a custom error handler like this in VB.NET: .... try ... Throw ("Lender Name not in table") .... catch ("Lender Name not in table")
6
by: Lance | last post by:
hi all, using the following code, i never get a message box showing the error, but the default error sound is produced (like there should be an accompanying messagebox). \\\\\\\\\\ Dim FI As...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.