473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Build-in or user-defined exceptions?

Hallöchen!

I write a module, and so I must raise exceptions at several points.
The PEP 8 says that a module should define their own exceptions base
class, derived from "Exception", and derivatives of that.

However, the build-in exceptions cover most of the error types that
occur in a standard program. For example, my module communicates
with measurement instruments, so any communication error would fit
perfectly to the build-in "IOError", wouldn't it? Invalid arguments
can raise "TypeError"s, and so on and so forth.

Is it considered good style to raise build-in exceptions whereever
one thinks it's appropriate? Is the definition of module exceptions
derived from "IOError", "TypeError" etc. a good idea? Is there some
sort of style guide for recommended exceptions design in Python?

Thank you!

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jul 19 '05 #1
2 2359
Torsten Bronger wrote:
Is it considered good style to raise build-in exceptions whereever
one thinks it's appropriate?
yes.
Is the definition of module exceptions derived from "IOError", "TypeError"
etc. a good idea?
sure (when absolutely necessary).
Is there some sort of style guide for recommended exceptions design in
Python?


just use your intuition.

</F>

Jul 19 '05 #2
Torsten Bronger wrote:
I write a module, and so I must raise exceptions at several points.
The PEP 8 says that a module should define their own exceptions base
class, derived from "Exception", and derivatives of that.

However, the build-in exceptions cover most of the error types that
occur in a standard program. For example, my module communicates
with measurement instruments, so any communication error would fit
perfectly to the build-in "IOError", wouldn't it?
No... *any* communication error? What if it's an error in the serial
port layer, or the protocol layer, or the application layer handling
that communication? Wouldn't SerialError, ProtocolError, or
UnrecognizedResponseError (or things like that) be much more readable.
I find fine-grained app-specific exceptions to be very helpful in
writing code that talks to machines.
Invalid arguments
can raise "TypeError"s, and so on and so forth.
Likewise, RangeError, InvalidAddressError, and the like seem more
helpful to me, most of the time.

I guess I pretty much follow PEP8 (though I don't recall its details
right now) since I derive from Exception things like DriverError and
then derive more specific exceptions from that as needed (in the case of
a driver module), or ProtocolError and children, etc..
Is it considered good style to raise build-in exceptions whereever
one thinks it's appropriate? Is the definition of module exceptions
derived from "IOError", "TypeError" etc. a good idea? Is there some
sort of style guide for recommended exceptions design in Python?


As Fredrik recommends, use your own judgment, but I believe that one
should use a very narrow definition for those exceptions. For example,
TypeError should be used specifically when the code involved has
received an object of the wrong type and *not* (for example) when a
driver receives the wrong "type" of command from another device.

I sometimes derive from things like ValueError (rather than just
Exception) when it seems appropriate, but that's pretty rare, and it's
also not what the docs themselves recommend.

Be guided by the descriptions of those exceptions in the docs and not by
their names alone. See
http://docs.python.org/lib/module-exceptions.html for more and be sure
to read the descriptions of the base classes, not just the subclasses.

-Peter
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steve Holden | last post by:
Does anyone know the workaround for this error, encountered when trying to build PIL 1.1.4 from source under cygwin (Python 2.3.3)? running build_ext building '_imaging' extension gcc...
8
by: ted | last post by:
How does the speed of the Scons build tool compare with Ant? Right now with out Ant builds take around an hour. Hoping to speed that up. TIA, Ted
0
by: Scott Auge | last post by:
I created this script to help make zip files for software distributions. It will: -- Create a build number -- Create a VERSION file so people know which build they have -- Update the...
2
by: Felix | last post by:
Out of no where I'm starting to get these problems and my Deployment projects won't build anymore. Does anyone have any idea what's causing this? I've tried copying these files from another...
0
by: greg | last post by:
I created deployment project for my web application (to install on the same computer so I dont need to add runtime) Added project output AND content files would not build content...
0
by: Andrew Ayre | last post by:
Hi, I can't seem to get the library built, and any help is greatly appreciated. Here is the info: Windows XP Borland C++ Builder 5 Latest Boost source code (downloaded at the weekend) Windows...
3
by: michael.lang | last post by:
I am using PostBuild events in a C# project to run some tasks, but only when in release configuration mode. The build events run perfectly as expected when run. The problem comes when I save the...
1
by: Ted Zeng | last post by:
Hi, I am new to Python. I am trying to build MySQLdb for my PowerPC PowerMac. I just downloaded the source last week and tried to build it. But I got the error messages as follow. I checked and...
1
by: John Dalberg | last post by:
The "Build Web Site" build option seems to do a complete build even when no changes have been made. Is there a way to do a build only when a source file has changed? This is a web site app. John...
0
by: Jack | last post by:
Since I don't have VS2003, I'm trying to build cjson with MingW and Cygwin but I'm getting lots of errors like these: build\temp.win32-2.5\Release\cjson.o(.text+0x8e):cjson.c: undefined...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.