try: except <never>:

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hallvard B Furuseth

    #1

    try: except <never>:

    I'd like an 'except <exception which is never raised>' statement
    Is there a defined way to do that, for Python 2.2 and above?
    'except None:' works for now, but I don't know if that's safe:

    for ex in ZeroDivisionErr or, None:
    try:
    1/0
    except ex:
    print "Ignored first exception."

    I could just use
    except ZeroDivisionErr or:
    if not <first iteration>:
    raise
    print "Ignored first exception."
    but the variant above gets a bit neater.

    --
    Hallvard
  • Paul Rubin

    #2
    Re: try: except &lt;never&gt ;:

    Hallvard B Furuseth <h.b.furuseth@u sit.uio.no> writes:[color=blue]
    > 'except None:' works for now, but I don't know if that's safe:
    >
    > for ex in ZeroDivisionErr or, None:
    > try:
    > 1/0
    > except ex:
    > print "Ignored first exception."[/color]

    class NeverRaised(Exc eption): pass

    for ex in ZeroDivisionErr or, NeverRaised:
    ...

    Comment

    • Hallvard B Furuseth

      #3
      Re: try: except &lt;never&gt ;:

      Paul Rubin writes:[color=blue]
      >Hallvard B Furuseth <h.b.furuseth@u sit.uio.no> writes:[color=green]
      >> 'except None:' works for now, but I don't know if that's safe:
      >>
      >> for ex in ZeroDivisionErr or, None:
      >> try:
      >> 1/0
      >> except ex:
      >> print "Ignored first exception."[/color]
      >
      > class NeverRaised(Exc eption): pass
      >
      > for ex in ZeroDivisionErr or, NeverRaised:[/color]

      Heh. Simple enough. Unless some obstinate person raises it anyway...

      --
      Hallvard

      Comment

      • Paul Rubin

        #4
        Re: try: except &lt;never&gt ;:

        Hallvard B Furuseth <h.b.furuseth@u sit.uio.no> writes:[color=blue][color=green]
        > > class NeverRaised(Exc eption): pass
        > > for ex in ZeroDivisionErr or, NeverRaised:[/color]
        >
        > Heh. Simple enough. Unless some obstinate person raises it anyway...[/color]

        Hmm, ok, how's this?:

        def NeverRaised():
        class blorp(Exception ): pass
        return blorp
        for ex in ZeroDivisionErr or, NeverRaised():
        ...

        Comment

        • Duncan Booth

          #5
          Re: try: except &lt;never&gt ;:

          Paul Rubin wrote:
          [color=blue]
          > Hallvard B Furuseth <h.b.furuseth@u sit.uio.no> writes:[color=green][color=darkred]
          >> > class NeverRaised(Exc eption): pass
          >> > for ex in ZeroDivisionErr or, NeverRaised:[/color]
          >>
          >> Heh. Simple enough. Unless some obstinate person raises it anyway...[/color]
          >
          > Hmm, ok, how's this?:
          >
          > def NeverRaised():
          > class blorp(Exception ): pass
          > return blorp
          > for ex in ZeroDivisionErr or, NeverRaised():
          > ...[/color]

          Or you can create an unraisable exception:
          [color=blue][color=green][color=darkred]
          >>> \[/color][/color][/color]
          class NeverRaised(Exc eption):
          def __init__(self, *args):
          raise RuntimeError('N everRaised should never be raised')
          [color=blue][color=green][color=darkred]
          >>> \[/color][/color][/color]
          try:
          raise NeverRaised
          except NeverRaised:
          print "caught it"



          Traceback (most recent call last):
          File "<pyshell#4 7>", line 4, in __init__
          raise RuntimeError('N everRaised should never be raised')
          RuntimeError: NeverRaised should never be raised

          Comment

          • Tom Anderson

            #6
            Re: try: except &lt;never&gt ;:

            On Tue, 10 Jan 2006, Duncan Booth wrote:
            [color=blue]
            > Paul Rubin wrote:
            >[color=green]
            >> Hallvard B Furuseth <h.b.furuseth@u sit.uio.no> writes:[color=darkred]
            >>>> class NeverRaised(Exc eption): pass
            >>>> for ex in ZeroDivisionErr or, NeverRaised:
            >>>
            >>> Heh. Simple enough. Unless some obstinate person raises it anyway...[/color]
            >>
            >> Hmm, ok, how's this?:
            >>
            >> def NeverRaised():
            >> class blorp(Exception ): pass
            >> return blorp
            >> for ex in ZeroDivisionErr or, NeverRaised():
            >> ...[/color][/color]

            Nice.
            [color=blue]
            > Or you can create an unraisable exception:
            >[color=green][color=darkred]
            >>>> \[/color][/color]
            > class NeverRaised(Exc eption):
            > def __init__(self, *args):
            > raise RuntimeError('N everRaised should never be raised')[/color]

            Briliant! Although i'd be tempted to define an UnraisableExcep tionError to
            signal what's happened. Or ...

            class ImpossibleExcep tion(Exception) :
            def __init__(self, *args):
            raise ImpossibleExcep tion, args

            Although crashing the interpreter is probably overkill.

            tom

            --
            Like Kurosawa i make mad films; okay, i don't make films, but if i did
            they'd have a samurai.

            Comment

            • Duncan Booth

              #7
              Re: try: except &lt;never&gt ;:

              Tom Anderson wrote:
              [color=blue][color=green]
              >> class NeverRaised(Exc eption):
              >> def __init__(self, *args):
              >> raise RuntimeError('N everRaised should never be raised')[/color]
              >
              > Briliant! Although i'd be tempted to define an
              > UnraisableExcep tionError to signal what's happened. Or ...
              >
              > class ImpossibleExcep tion(Exception) :
              > def __init__(self, *args):
              > raise ImpossibleExcep tion, args
              >
              > Although crashing the interpreter is probably overkill.[/color]

              Crashng the interpreter would be, but what you just wrote is simply a more
              obscure way of raising RuntimeError :-)
              [color=blue][color=green][color=darkred]
              >>> class ImpossibleExcep tion(Exception) :[/color][/color][/color]
              .... def __init__(self, *args):
              .... raise ImpossibleExcep tion, args
              ....[color=blue][color=green][color=darkred]
              >>> raise ImpossibleExcep tion[/color][/color][/color]
              Traceback (most recent call last):
              File "<stdin>", line 3, in __init__
              RuntimeError: maximum recursion depth exceeded

              Comment

              • Hallvard B Furuseth

                #8
                Re: try: except &lt;never&gt ;:

                Thanks for the help.

                Tom Anderson writes:[color=blue][color=green]
                >> class NeverRaised(Exc eption):
                >> def __init__(self, *args):
                >> raise RuntimeError('N everRaised should never be raised')[/color]
                >
                > Briliant! Although i'd be tempted to define an UnraisableExcep tionError
                > to signal what's happened. Or ...[/color]

                A package we are using has ProgrammingErro r (like AssertionError except
                __debug__ doesn't disable it), and RealityError when something *really*
                can't happen:-)

                --
                Hallvard

                Comment

                Working...