>>>>> David House <dmho...@gmail.com> (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 <p...@cs.uu.nl> URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list