new style exception handleing

O

Ola Natvig

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.

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 ?
 
P

Peter Hansen

Ola said:
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.

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
 
O

Ola Natvig

Peter said:
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.
 
M

Michele Simionato

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
 
N

Nick Coghlan

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

Basically because fixing it without killing backward compatibility with string
exceptions is hard :)

Doesn't mean people aren't trying though (Google the python-dev archives, as
well as the python-list ones).

Cheers,
Nick.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top