473,396 Members | 1,666 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,396 software developers and data experts.

Exceptions as New Style Classes

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 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
class OldException: pass
raise OldException() Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.OldException: <__main__.OldException instance at 0x006AC490>
class NewException(object): pass
raise NewException()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: exceptions must be classes, instances, or strings
(deprecated), not NewException
Jul 18 '05 #1
6 2011
Quoth Andrew:
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?


It is intentional.

There are two difficulties with allowing new-style classes as
exception types. Ideas exist for dealing with them, but at the
moment none of them as been implemented.

Problem 1: Implicit instantiation. When the interpreter sees
raise x
it must determine whether x is a class or an instance, so it can
determine whether to instantiate it. (E.g., in
x = TypeError
raise x
instantiation will occur during the raise statement, whereas in
x = TypeError()
raise x
it will not.) The problem is that, with new-style classes, there
is no clear and strong distinction between classes and instances,
so it is not clear how the interpreter can make this decision.

Problem 2: It is probably not desirable to allow things like
raise 3
raise {'d': 17}
since raising such objects as ints and dicts (etc.) is very
unlikely to be anything but a bug in practice. (There are cases
in which you might want to do such things, but they are rare.)
However, there is (or will and should be) no clear and strong
distinction between, say, user-defined new-style classes and
built-in types, so it is not clear how the interpreter would
detect such cases and forbid them.

At the moment the dominant idea for solving these problems is to
make inheritance from Exception mandatory for exception types; see
Guido's 11 June post
<http://mail.python.org/pipermail/python-dev/2003-June/036162.html>
for details. (I have another idea involving a new special method
'__raise__', but haven't worked out the details yet.)

--
Steven Taschuk w_w
st******@telusplanet.net ,-= U
1 1

Jul 18 '05 #2
Steven Taschuk wrote:
At the moment the dominant idea for solving these problems is to
make inheritance from Exception mandatory for exception types; see
Guido's 11 June post
<http://mail.python.org/pipermail/python-dev/2003-June/036162.html>
for details. (I have another idea involving a new special method
'__raise__', but haven't worked out the details yet.)

Why not give exceptions their own metaclass? So if type(x) is
ExceptionMetaclass, it's a class. If type(type(x)) is
ExceptionMetaclass, it's an instance. Otherwise, it's illegal to
raise it.
--
CARL BANKS

Jul 18 '05 #3
Quoth Carl Banks:
Steven Taschuk wrote:
At the moment the dominant idea for solving these problems is to
make inheritance from Exception mandatory for exception types; see
[...] Why not give exceptions their own metaclass? So if type(x) is
ExceptionMetaclass, it's a class. If type(type(x)) is
ExceptionMetaclass, it's an instance. Otherwise, it's illegal to
raise it.


That seems workable. (I'd prefer, though, to test
isinstance(type(x), ExceptionMetaclass)
and likewise for the second case.)

I'm not sure what it gains us, though, over the idea of mandatory
inheritance from Exception. Am I missing something?

--
Steven Taschuk st******@telusplanet.net
"Telekinesis would be worth patenting." -- James Gleick

Jul 18 '05 #4
Steven Taschuk wrote:
Quoth Carl Banks:
Steven Taschuk wrote:
> At the moment the dominant idea for solving these problems is to
> make inheritance from Exception mandatory for exception types; see

[...]
Why not give exceptions their own metaclass? So if type(x) is
ExceptionMetaclass, it's a class. If type(type(x)) is
ExceptionMetaclass, it's an instance. Otherwise, it's illegal to
raise it.


That seems workable. (I'd prefer, though, to test
isinstance(type(x), ExceptionMetaclass)
and likewise for the second case.)

I'm not sure what it gains us, though, over the idea of mandatory
inheritance from Exception. Am I missing something?


Probably not much, unless you think you want to raise exceptions that
aren't subclasses of Exception.
--
CARL BANKS
Jul 18 '05 #5
Steven Taschuk <st******@telusplanet.net> writes:
Quoth Andrew:
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?


It is intentional.

There are two difficulties with allowing new-style classes as
exception types. Ideas exist for dealing with them, but at the
moment none of them as been implemented.


They have, in PyPy! (which doesn't do old-style classes at all, at
present).

IIUC, Guido thought what PyPy does would be sensible enough for
CPython, but I don't know what the timeframe might be.

Cheers,
mwh

--
This is not to say C++ = bad, Lisp = good. It's to say
C++ = bad irrespective of everything else.
-- Alain Picard, comp.lang.lisp
Jul 18 '05 #6
Quoth Carl Banks:
Steven Taschuk wrote:
Quoth Carl Banks: [...]
Why not give exceptions their own metaclass? So if type(x) is
ExceptionMetaclass, it's a class. If type(type(x)) is
ExceptionMetaclass, it's an instance. Otherwise, it's illegal to
raise it.
[...] I'm not sure what it gains us, though, over the idea of mandatory
inheritance from Exception. Am I missing something?


Probably not much, unless you think you want to raise exceptions that
aren't subclasses of Exception.


I would have thought that having to raise instances of classes
which are instances of ExceptionMetaclass would be just as onerous
as having to raise instances of Exception.

class MyRaisable(Exception):
# ...

class MyRaisable(object):
__metaclass__ = ExceptionMetaclass
# ...

I suppose Exception could have behaviour you don't want, in which
case the latter might be preferable.

--
Steven Taschuk st******@telusplanet.net
"Our analysis begins with two outrageous benchmarks."
-- "Implementation strategies for continuations", Clinger et al.

Jul 18 '05 #7

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

Similar topics

21
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
2
by: Torsten Bronger | last post by:
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...
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...
10
by: Razzie | last post by:
Hi all, The title of this post may sound a bit weird, but I was wondering about the following nonetheless. I have a class libray containing, say, 4 classes: A, B, C, D. Class A somehow has a...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
28
by: Christoph Zwerschke | last post by:
What is the best way to re-raise any exception with a message supplemented with additional information (e.g. line number in a template)? Let's say for simplicity I just want to add "sorry" to every...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
14
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but...
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:
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
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...
0
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...

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.