Exceptions as New Style Classes

A

Andrew

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
win32Traceback (most recent call last):
File "<stdin>", line 1, in ?
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: exceptions must be classes, instances, or strings
(deprecated), not NewException
 
S

Steven Taschuk

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.)
 
C

Carl Banks

Steven said:
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.
 
S

Steven Taschuk

Quoth Carl Banks:
Steven said:
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?
 
C

Carl Banks

Steven said:
Quoth Carl Banks:
Steven said:
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.
 
M

Michael Hudson

Steven Taschuk said:
Quoth Andrew:

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
 
S

Steven Taschuk

Quoth Carl Banks:
Steven said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top