Conditional except: blocks

Discussion in 'Python' started by Robert Brewer, Jan 2, 2004.

  1. I don't have a quesiton or problem, I just figured some of you would
    enjoy the somewhat bizarre logic of the snippet I just wrote for a CGI
    request dispatcher:

    try:
    return nextHandler(args)
    except (None, Exception)[self.trapErrors], exc:
    if page == u'-Error':
    raise exc
    else:
    return self.handle_error(exc, scriptWithPath)

    If self.trapErrors is True, all exceptions (which subclass Exception)
    get trapped, otherwise, no exceptions are trapped. Spiffy. :)


    FuManChu
     
    Robert Brewer, Jan 2, 2004
    #1
    1. Advertising

  2. Robert Brewer

    Aahz Guest

    In article <>,
    Robert Brewer <> wrote:
    >
    >I don't have a quesiton or problem, I just figured some of you would
    >enjoy the somewhat bizarre logic of the snippet I just wrote for a CGI
    >request dispatcher:
    >
    >try:
    > return nextHandler(args)
    >except (None, Exception)[self.trapErrors], exc:
    > if page =3D=3D u'-Error':
    > raise exc
    > else:
    > return self.handle_error(exc, scriptWithPath)
    >
    >If self.trapErrors is True, all exceptions (which subclass Exception)
    >get trapped, otherwise, no exceptions are trapped. Spiffy. :)


    That's pretty sick. If you really need to do that, here's a more
    Pythonic approach:

    # This goes at beginning of program
    # or maybe at class/instance initialization
    if trapErrors:
    exceptTuple = (Exception,)
    else:
    exceptTuple = (None,)

    # some time later
    try:
    return nextHandler(args)
    except exceptTuple, exc:
    # Handle exception
    --
    Aahz () <*> http://www.pythoncraft.com/

    Weinberg's Second Law: If builders built buildings the way programmers wrote
    programs, then the first woodpecker that came along would destroy civilization.
     
    Aahz, Jan 2, 2004
    #2
    1. Advertising

  3. Robert Brewer

    sdd Guest

    Robert Brewer wrote:
    > try:
    > return nextHandler(args)
    > except (None, Exception)[self.trapErrors], exc:
    > if page == u'-Error':
    > raise exc

    raise # re-raises the same error with the full traceback.
    > else:
    > return self.handle_error(exc, scriptWithPath)
     
    sdd, Jan 2, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno
    Replies:
    20
    Views:
    880
    John Salerno
    Aug 11, 2006
  2. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    397
  3. David House

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    363
    Bruno Desthuilliers
    Jul 6, 2009
  4. Peng Yu
    Replies:
    1
    Views:
    561
    Steven D'Aprano
    Nov 18, 2009
  5. matt
    Replies:
    1
    Views:
    295
    George Ogata
    Aug 6, 2004
Loading...

Share This Page