473,699 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suggesting the use of StandardError as base of error Exceptions.

In a number of cases I have a program that looks like the following.

for case in all_cases:
try:
treat(case)
except Exception, ErrInfo:
generate_traceb ack()

The idea is to get as much information as possible when something
goes wrong but at the same time treat as many cases as possible.

Then one day things broke. The reason was that in some circumstances
treat would decide that things were beyond control and called sys.exit
However sys.exit doesn't return to the O.S. immediately but raises
SystemExit, which was caugth by the code and the loop continued.

I then took a look at http://docs.python.org/lib/module-exceptions.html
which describes the exception heirarchy as follows:

Exception
+-- SystemExit
+-- StopIteration
+-- StandardError
| +
| + All kind of error exceptions
| +
+---Warning
+
+ All kind of warnings
+

and came to the conclusion, that it would be better to write my code
as follows:

for case in all_cases:
try:
treat(case)
except StandardError, ErrInfo:
generate_traceb ack()

Unfortunatly this doesn't work either because a lot of the error
exceptions in the stdlib (if not all) inherit directly from
Exception instead of from StandardError. The documentation also
seems to suggest this use for users exception.

Now I was wondering if it wouldn't be better that for exception
that indicate some error condition that these would inherit
from StandardError and that this would be indicated in the
documentation and reflected in the stdlib?

Would it break much code to make this change? My first impression
would be no, but I could be missing something.

--
Antoon Pardon
Mar 6 '06 #1
3 1418
Antoon Pardon schrieb:
In a number of cases I have a program that looks like the following.

for case in all_cases:
try:
treat(case)
except Exception, ErrInfo:
generate_traceb ack()

The idea is to get as much information as possible when something
goes wrong but at the same time treat as many cases as possible.

Then one day things broke. The reason was that in some circumstances
treat would decide that things were beyond control and called sys.exit
However sys.exit doesn't return to the O.S. immediately but raises
SystemExit, which was caugth by the code and the loop continued.

I then took a look at http://docs.python.org/lib/module-exceptions.html
which describes the exception heirarchy as follows:

Exception
+-- SystemExit
+-- StopIteration
+-- StandardError
| +
| + All kind of error exceptions
| +
+---Warning
+
+ All kind of warnings
+

and came to the conclusion, that it would be better to write my code
as follows:

for case in all_cases:
try:
treat(case)
except StandardError, ErrInfo:
generate_traceb ack()

Unfortunatly this doesn't work either because a lot of the error
exceptions in the stdlib (if not all) inherit directly from
Exception instead of from StandardError. The documentation also
seems to suggest this use for users exception.

Now I was wondering if it wouldn't be better that for exception
that indicate some error condition that these would inherit
from StandardError and that this would be indicated in the
documentation and reflected in the stdlib?

Would it break much code to make this change? My first impression
would be no, but I could be missing something.


There has been a discussion on this just a few days ago, have you read that? There is even a PEP mentioned.

http://groups.google.com/group/comp....403ae0c6267ca1
Diez

Mar 6 '06 #2
Diez B. Roggisch wrote:
Antoon Pardon schrieb:
Now I was wondering if it wouldn't be better that for exception
that indicate some error condition that these would inherit
from StandardError and that this would be indicated in the
documentation and reflected in the stdlib?

Would it break much code to make this change? My first impression
would be no, but I could be missing something.

There has been a discussion on this just a few days ago, have you read
that? There is even a PEP mentioned.


It's PEP 352, it's already done. This will be in Python 2.5. It's good
to see that the time machine is stil working ;)
http://www.python.org/peps/pep-0352.html

Kent
Mar 6 '06 #3
Antoon Pardon wrote:
I then took a look at http://docs.python.org/lib/module-exceptions.html
which describes the exception heirarchy as follows:

Exception
+-- SystemExit
+-- StopIteration
+-- StandardError
| +
| + All kind of error exceptions
| +
+---Warning
+
+ All kind of warnings
+

and came to the conclusion, that it would be better to write my code
as follows:

for case in all_cases:
try:
treat(case)
except StandardError, ErrInfo:
generate_traceb ack()


You've already been pointed to `PEP 352`_, but just to highlight the
salient point, I believe what you want to write is::

try:
...
except (KeyboardInterr upt, SystemExit):
raise
except:
...

... _PEP 352: http://www.python.org/peps/pep-0352.html

STeVe
Mar 6 '06 #4

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

Similar topics

0
412
by: John Lewin | last post by:
I've recently discovered the value of using existing console applications in managed .net apps. Unfortunately, I've stumbled into a problem with a particular console application that check's crc info on CDs. Using either the StandardError or StandardOutput streams read, readline or peek methods causes an immediate block that doesn't release until the console app finishes it's business. I want to launch this console app and update my UI to...
2
2258
by: mwazir | last post by:
Hi all, I have reposted this question from dotnet.general as I have been advised that this is a more appropriate forum for this question. Apologies for the repost. I have a process thats starts in my application and only terminates when my application is terminated. I want to write the output and the errors of this process to a seperate log file. In order to do this, I spawned two threads.
6
1837
by: Ben Finney | last post by:
Howdy all, Okay, so Guido doesn't like Abstract Base Classes, and interfaces are the way of the future. But they're not here now, and I understand ABCs better. I want my modules to (sometimes) define an abstract base exception class, that all other exceptions in that module inherit from. class FooException(Exception):
4
1423
by: Tony | last post by:
I am in the process of setting up a base page model for multiple reasons. One of the reasons is so that I can catch all exceptions when derived pages throw/raise them. I don't want to use the standard OnError virtual method to handle this requirement because the form does not complete its rendering process when this occurs. I really need the form to display properly on the screen. I have managed to catch nearly all exceptions by...
5
3264
by: hillcountry74 | last post by:
Hi, I'm a newbie to OOP and VB.Net. In the business layer, I have a base class, which is an abstract class in VB.Net. The derived class overrides a method and calls other methods in the base class. To make it more clear: Class NotInheritable BaseAbstract Sub Method1() code
7
2241
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
0
1134
by: Andrew | last post by:
Hello, My question is this: You have an application(parent) which creates a child process and redirects its StandardOutput and StandardError To avoid deadlock 2 threads are used to read from the child process, each thread reading a stream. Is it thread safe to use the Process object and read the 2 streams ? if yes can you please say why ?
35
3780
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
3
2541
by: robiman | last post by:
Hi, I'm running an external program using System.Diagnostics Process. This program creates a PDF file from a CAD file via commandline "me10f.exe - p filename.mi". But in case of errors the program reports this with a messagebox, not on the console. Can someone tell me if it is possible to: - disable/hide the popup errors from external app? - catch the error messages via standarderror or some other way?
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.