try -> except -> else -> except?

Discussion in 'Python' started by David House, Jul 6, 2009.

  1. David House

    David House Guest

    Hi all,

    I'm looking for some structure advice. I'm writing something that
    currently looks like the following:

    try:
    <short amount of code that may raise a KeyError>
    except KeyError:
    <error handler>
    else:
    <nontrivial amount of code>

    This is working fine. However, I now want to add a call to a function
    in the `else' part that may raise an exception, say a ValueError. So I
    was hoping to do something like the following:

    try:
    <short amount of code that may raise a KeyError>
    except KeyError:
    <error handler>
    else:
    <nontrivial amount of code>
    except ValueError:
    <error handler>

    However, this isn't allowed in Python.

    An obvious way round this is to move the `else' clause into the `try', i.e.,

    try:
    <short amount of code that may raise a KeyError>
    <nontrivial amount of code>
    except KeyError:
    <error handler>
    except ValueError:
    <error handler>

    However, I am loath to do this, for two reasons:

    (i) if I modify the <nontrivial amount of code> block at some point in
    the future so that it may raise a KeyError, I have to somehow tell
    this exception from the one that may be generated from the <short
    amount of code that may raise a KeyError> line.
    (ii) it moves the error handler for the <short amount of code that may
    raise a KeyError> bit miles away from the line that might generate the
    error, making it unclear which code the KeyError error handler is an
    error handler for.

    What would be the best way to structure this?

    --
    -David
     
    David House, Jul 6, 2009
    #1
    1. Advertising

  2. >>>>> David House <> (DH) wrote:

    >DH> Hi all,
    >DH> I'm looking for some structure advice. I'm writing something that
    >DH> currently looks like the following:


    >DH> try:
    >DH> <short amount of code that may raise a KeyError>
    >DH> except KeyError:
    >DH> <error handler>
    >DH> else:
    >DH> <nontrivial amount of code>


    >DH> This is working fine. However, I now want to add a call to a function
    >DH> in the `else' part that may raise an exception, say a ValueError. So I
    >DH> was hoping to do something like the following:


    >DH> try:
    >DH> <short amount of code that may raise a KeyError>
    >DH> except KeyError:
    >DH> <error handler>
    >DH> else:
    >DH> <nontrivial amount of code>
    >DH> except ValueError:
    >DH> <error handler>


    >DH> However, this isn't allowed in Python.


    >DH> An obvious way round this is to move the `else' clause into the `try', i.e.,


    >DH> try:
    >DH> <short amount of code that may raise a KeyError>
    >DH> <nontrivial amount of code>
    >DH> except KeyError:
    >DH> <error handler>
    >DH> except ValueError:
    >DH> <error handler>


    >DH> However, I am loath to do this, for two reasons:


    >DH> (i) if I modify the <nontrivial amount of code> block at some point in
    >DH> the future so that it may raise a KeyError, I have to somehow tell
    >DH> this exception from the one that may be generated from the <short
    >DH> amount of code that may raise a KeyError> line.
    >DH> (ii) it moves the error handler for the <short amount of code that may
    >DH> raise a KeyError> bit miles away from the line that might generate the
    >DH> error, making it unclear which code the KeyError error handler is an
    >DH> error handler for.


    >DH> What would be the best way to structure this?


    try:
    <short amount of code that may raise a KeyError>
    except KeyError:
    <error handler>
    else:
    try:
    <nontrivial amount of code>
    except ValueError:
    <error handler>

    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 6, 2009
    #2
    1. Advertising

  3. David House a écrit :
    > Hi all,
    >
    > I'm looking for some structure advice. I'm writing something that
    > currently looks like the following:
    >
    > try:
    > <short amount of code that may raise a KeyError>
    > except KeyError:
    > <error handler>
    > else:
    > <nontrivial amount of code>
    >
    > This is working fine. However, I now want to add a call to a function
    > in the `else' part that may raise an exception, say a ValueError.


    If your error handler terminates the function (which is usually the case
    when using the else clause), you can just skip the else statement, ie:

    try:
    <short amount of code that may raise a KeyError>
    except KeyError:
    <error handler with early exit>
    <nontrivial amount of code>

    Then adding one or more try/except is just trivial.


    > So I
    > was hoping to do something like the following:
    >
    > try:
    > <short amount of code that may raise a KeyError>
    > except KeyError:
    > <error handler>
    > else:
    > <nontrivial amount of code>
    > except ValueError:
    > <error handler>
    >
    > However, this isn't allowed in Python.


    Nope. But this is legal:


    try:
    <short amount of code that may raise a KeyError>
    except KeyError:
    <error handler>
    else:
    try:
    <nontrivial amount of code>
    except ValueError:
    <error handler>



    > An obvious way round this is to move the `else' clause into the `try'


    "obvious" but not necessarily the best thing to do.

    (snip - cf above for simple answers)
     
    Bruno Desthuilliers, Jul 6, 2009
    #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:
    4
    Views:
    401
    John Salerno
    Jul 28, 2006
  2. John Salerno
    Replies:
    20
    Views:
    859
    John Salerno
    Aug 11, 2006
  3. Ed Jensen

    try/except/else/finally problem

    Ed Jensen, Jun 28, 2007, in forum: Python
    Replies:
    5
    Views:
    448
    Ben Finney
    Jun 29, 2007
  4. Fabio Z Tessitore

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

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    378
  5. kj
    Replies:
    15
    Views:
    560
    Lawrence D'Oliveiro
    May 23, 2009
Loading...

Share This Page