catching exceptions from an except: block

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriel Genellina

    #31
    Re: catching exceptions from an except: block

    En Fri, 09 Mar 2007 04:49:59 -0300, Gerard Flanagan
    <grflanagan@yah oo.co.ukescribi ó:
    Another version:
    >
    import exceptions
    As back in time as I could go (Python 1.5), exceptions were available as
    builtins...
    def onfailFalse(fn) :
    def inner(*args, **kwargs):
    try:
    return fn(*args, **kwargs)
    except ABCException:
    return False
    return inner
    >
    @onfailFalse
    def a(x):
    if x == 1:
    return 'function a succeeded'
    else:
    raise ABCException()
    There is a serious flaw on this approach, the function can't return any
    false value (it would be treated as a failure).

    --
    Gabriel Genellina

    Comment

    • Gabriel Genellina

      #32
      Re: catching exceptions from an except: block

      En Fri, 09 Mar 2007 05:52:35 -0300, Duncan Booth
      <duncan.booth@i nvalid.invalide scribió:
      "Gabriel Genellina" <gagsl-py2@yahoo.com.a rwrote:
      >
      >Not the *previous* exception, but the *current* one. You must be
      >inside an "except" clause to use a bare raise.
      >>
      No, you don't have to be inside an except clause to use a bare raise.
      A bare 'raise' will re-raise the last exception that was active in the
      current scope. That applies even outside the except clauses just so long
      as there has been an exception within the same function:
      Oh! Thanks, I didn't know that.
      I tested it in the interpreter, outside any function, and the exception
      info was lost immediately, so I wrongly concluded that it lived shortly.

      --
      Gabriel Genellina

      Comment

      • Gerard Flanagan

        #33
        Re: catching exceptions from an except: block

        On Mar 9, 9:56 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
        En Fri, 09 Mar 2007 04:49:59 -0300, Gerard Flanagan
        <grflana...@yah oo.co.ukescribi ó:
        >
        Another version:
        >
        import exceptions
        >
        As back in time as I could go (Python 1.5), exceptions were available as
        builtins...
        >
        I did not know that. Thanks.
        def onfailFalse(fn) :
        def inner(*args, **kwargs):
        try:
        return fn(*args, **kwargs)
        except ABCException:
        return False
        return inner
        >
        @onfailFalse
        def a(x):
        if x == 1:
        return 'function a succeeded'
        else:
        raise ABCException()
        >
        There is a serious flaw on this approach, the function can't return any
        false value (it would be treated as a failure).
        >
        I was teaching myself decorators more than anything, so it's not
        thought out to any extent, but even so I don't think it's a "serious
        flaw", rather it would be programmer error to use @onfailFalse on a
        function that may return False. Don't you think?

        Gerard



        Comment

        • Gabriel Genellina

          #34
          Re: catching exceptions from an except: block

          En Fri, 09 Mar 2007 07:30:20 -0300, Gerard Flanagan
          <grflanagan@yah oo.co.ukescribi ó:
          >There is a serious flaw on this approach, the function can't return any
          >false value (it would be treated as a failure).
          >
          I was teaching myself decorators more than anything, so it's not
          thought out to any extent, but even so I don't think it's a "serious
          flaw", rather it would be programmer error to use @onfailFalse on a
          function that may return False. Don't you think?
          I thought this was on response to the original problem, not for your own
          problem.

          --
          Gabriel Genellina

          Comment

          • Gerard Flanagan

            #35
            Re: catching exceptions from an except: block

            On Mar 9, 11:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
            wrote:
            En Fri, 09 Mar 2007 07:30:20 -0300, Gerard Flanagan
            <grflana...@yah oo.co.ukescribi ó:
            >
            There is a serious flaw on this approach, the function can't return any
            false value (it would be treated as a failure).
            >
            I was teaching myself decorators more than anything, so it's not
            thought out to any extent, but even so I don't think it's a "serious
            flaw", rather it would be programmer error to use @onfailFalse on a
            function that may return False. Don't you think?
            >
            I thought this was on response to the original problem, not for your own
            problem.
            >
            Mea culpa.

            Gerard



            Comment

            • Gabriel Genellina

              #36
              Re: catching exceptions from an except: block

              En Fri, 09 Mar 2007 08:14:18 -0300, Gerard Flanagan
              <grflanagan@yah oo.co.ukescribi ó:
              Mea culpa.
              Ego te absolvo in nomine Patris Guidii et Filii Python et Spiritus Sancti
              Computatorium.

              --
              Gabriel Genellina

              Comment

              Working...