473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

new style exception handleing

Hi all

Does anybody know why it's not possible to raise Exceptions which are
types (new-style-classes). I know all standard exceptions are classic
classes, but if you make a custom exception which both inherits from a
exception class and a new-style one the it causes a type error when raised.
class b(Exception, object): pass raise b


Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
raise b
TypeError: exceptions must be classes, instances, or strings
(deprecated), not type

This is weird, I think. Are there any issues about raising types as
exceptions that I can't think of ?
--
--------------------------------------
Ola Natvig <ol********@infosense.no>
infoSense AS / development
Jul 18 '05 #1
3 1114
Ola Natvig wrote:
Does anybody know why it's not possible to raise Exceptions which are
types (new-style-classes). I know all standard exceptions are classic
classes, but if you make a custom exception which both inherits from a
exception class and a new-style one the it causes a type error when raised.
>>> class b(Exception, object): pass


This might not help you, but have you considered just making
your old-style class *contain a reference* to an instance
of whatever new-style class you want it to contain? Then
the issue goes away.

I can't actually think of a reason to need to base an
exception on a new-style class, but perhaps you have a
good one...

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
Ola Natvig wrote:
Does anybody know why it's not possible to raise Exceptions which are
types (new-style-classes). I know all standard exceptions are classic
classes, but if you make a custom exception which both inherits from a
exception class and a new-style one the it causes a type error when
raised.
>>> class b(Exception, object): pass

This might not help you, but have you considered just making
your old-style class *contain a reference* to an instance
of whatever new-style class you want it to contain? Then
the issue goes away.

I can't actually think of a reason to need to base an
exception on a new-style class, but perhaps you have a
good one...

-Peter


It's quite simple to bypass the problem, it was more the reason I was
wondering about too.

--
--------------------------------------
Ola Natvig <ol********@infosense.no>
infoSense AS / development
Jul 18 '05 #3
Google is your friend.
This has been discussed a lot in the past. For instance, google for the
thread,
"Exceptions as New Style Classes", there was also a PEP by Steven
Taschuk,
IIRC.

Michele Simionato

Jul 18 '05 #4

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

Similar topics

6
by: Andrew | last post by:
Hi, To satisfy my curiosity I was wondering if anyone knew if this behaviour was intentional? Is there a specific reason why exceptions are not allowed to be new style classes? Python 2.3...
29
by: Cheng Mo | last post by:
Recently I happens to read some sourcecode which has different style from my previous experience. In this style, all functions return a RETURN_TYPE_T which is defined as typedef unsigned int...
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
4
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView...
2
by: Mike Fiedler | last post by:
On a client web services request, I have a SoapExtension that is adding a header to the message in the BeforeSerialize stage. If the Web Service request is for an operation with...
1
by: ankit | last post by:
Hello, Please put some light on, What are new style classes and classic style classes in python. The basic differences in them. And How can I decide to choose one.
12
by: Mark Rae | last post by:
Hi, It's easy enough for a child page to manipulate its MasterPage's header simply by modifying the MasterPage thus: <header runat="server"> .... .... </header>
3
by: eefacm | last post by:
I have a class that derives from Exception. In Python 2.4, isinstance(MyClass(), types.InstanceType) was True. In 2.5, it's False. Further experimentation showed that derivation from object...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.