Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 09:59 PM
Ola Natvig
Guest
 
Posts: n/a
Default 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.
[color=blue][color=green][color=darkred]
>>> class b(Exception, object): pass[/color][/color][/color]
[color=blue][color=green][color=darkred]
>>> raise b[/color][/color][/color]

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 <ola.natvig@infosense.no>
infoSense AS / development
  #2  
Old July 18th, 2005, 10:00 PM
Peter Hansen
Guest
 
Posts: n/a
Default Re: new style exception handleing

Ola Natvig wrote:[color=blue]
> 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.
>[color=green][color=darkred]
> >>> class b(Exception, object): pass[/color][/color][/color]

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
  #3  
Old July 18th, 2005, 10:01 PM
Ola Natvig
Guest
 
Posts: n/a
Default Re: new style exception handleing

Peter Hansen wrote:[color=blue]
> Ola Natvig wrote:
>[color=green]
>> 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.
>>[color=darkred]
>> >>> class b(Exception, object): pass[/color][/color]
>
>
> 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[/color]

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

--
--------------------------------------
Ola Natvig <ola.natvig@infosense.no>
infoSense AS / development
  #4  
Old July 18th, 2005, 10:01 PM
Michele Simionato
Guest
 
Posts: n/a
Default Re: new style exception handleing

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles