new style exception handleing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ola Natvig

    #1

    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@inf osense.no>
    infoSense AS / development
  • Peter Hansen

    #2
    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

    Comment

    • Ola Natvig

      #3
      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@inf osense.no>
      infoSense AS / development

      Comment

      • Michele Simionato

        #4
        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

        Comment

        Working...