exceptions and items in a list

Discussion in 'Python' started by rbt, Jan 10, 2005.

  1. rbt

    rbt Guest

    If I have a Python list that I'm iterating over and one of the objects
    in the list raises an exception and I have code like this:

    try:
    do something to object in list
    except Exception:
    pass

    Does the code just skip the bad object and continue with the other
    objects in the list, or does it stop?

    Thanks
     
    rbt, Jan 10, 2005
    #1
    1. Advertising

  2. rbt wrote:
    > If I have a Python list that I'm iterating over and one of the objects
    > in the list raises an exception and I have code like this:
    >
    > try:
    > do something to object in list
    > except Exception:
    > pass
    >
    > Does the code just skip the bad object and continue with the other
    > objects in the list, or does it stop?


    # skip bad object and continue with others
    for object in objects:
    try:
    #do something to object
    except Exception:
    pass

    # stop at first bad object
    try:
    for object in objects:
    #do something to object
    except Exception:
    pass
     
    Andrey Tatarinov, Jan 10, 2005
    #2
    1. Advertising

  3. rbt wrote:
    > If I have a Python list that I'm iterating over and one of the objects
    > in the list raises an exception and I have code like this:
    >
    > try:
    > do something to object in list
    > except Exception:
    > pass
    >
    > Does the code just skip the bad object and continue with the other
    > objects in the list, or does it stop?
    >
    > Thanks


    Fire up a shell and try:

    >>> seq = ["1", "2", "a", "4", "5", 6.0]
    >>> for elem in seq:

    .... try:
    .... print int(elem)
    .... except ValueError:
    .... pass


    and see what happens...

    --
    Vincent Wehren
     
    vincent wehren, Jan 10, 2005
    #3
  4. rbt

    rbt Guest

    Andrey Tatarinov wrote:
    > rbt wrote:
    >
    >> If I have a Python list that I'm iterating over and one of the objects
    >> in the list raises an exception and I have code like this:
    >>
    >> try:
    >> do something to object in list
    >> except Exception:
    >> pass
    >>
    >> Does the code just skip the bad object and continue with the other
    >> objects in the list, or does it stop?

    >
    >
    > # skip bad object and continue with others
    > for object in objects:
    > try:
    > #do something to object
    > except Exception:
    > pass
    >
    > # stop at first bad object
    > try:
    > for object in objects:
    > #do something to object
    > except Exception:
    > pass


    Thanks Andrey. That's a great example of how to do it.
     
    rbt, Jan 10, 2005
    #4
  5. rbt

    Peter Hansen Guest

    rbt wrote:
    > Andrey Tatarinov wrote:
    >> # skip bad object and continue with others
    >> for object in objects:
    >> try:
    >> #do something to object
    >> except Exception:
    >> pass

    >
    > Thanks Andrey. That's a great example of how to do it.


    Actually, it's not really a "great" example, since it catches
    _all_ exceptions that might happen, and quietly ignores
    them. For example, in the following code, you might not
    realize that you've made a typo and none of the items in
    the list are actually being checked, even though there is
    obviously no problem with any of these simple items
    (integers from 0 to 9).

    def fornat_value(val):
    return '-- %5d --' % val

    L = range(10)
    for val in L:
    try:
    print format_value(val)
    except Exception:
    pass

    It's almost always better to identify the *specific*
    exceptions which you are expecting and to catch those,
    or not do a simple "pass". See Vincent's response,
    for example, where he explicitly asks only for ValueErrors
    and lets others propagate upwards, to be reported.

    (I realize these were contrived examples, but examples
    that don't mention this important issue risk the propagation
    of buggy code...)

    -Peter
     
    Peter Hansen, Jan 11, 2005
    #5
  6. rbt

    Steve Holden Guest

    vincent wehren wrote:

    > rbt wrote:
    >
    >> If I have a Python list that I'm iterating over and one of the objects
    >> in the list raises an exception and I have code like this:
    >>
    >> try:
    >> do something to object in list
    >> except Exception:
    >> pass
    >>
    >> Does the code just skip the bad object and continue with the other
    >> objects in the list, or does it stop?
    >>
    >> Thanks

    >
    >
    > Fire up a shell and try:
    >
    > >>> seq = ["1", "2", "a", "4", "5", 6.0]
    > >>> for elem in seq:

    > .... try:
    > .... print int(elem)
    > .... except ValueError:
    > .... pass
    >
    >
    > and see what happens...
    >
    > --
    > Vincent Wehren


    I suspect the more recent versions of Python allow a much more elegant
    solution. I can't remember precisely when we were allowed to use
    continue in an except suite, but I know we couldn't in Python 2.1.

    Nowadays you can write:

    Python 2.4 (#1, Dec 4 2004, 20:10:33)
    [GCC 3.3.3 (cygwin special)] on cygwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> for i in [1, 2, 3]:

    ... try:
    ... print i
    ... if i == 2: raise AttributeError, "Bugger!"
    ... except AttributeError:
    ... print "Caught exception"
    ... continue
    ...
    1
    2
    Caught exception
    3
    >>>


    To terminate the loop on the exception you would use "break" instead of
    "continue".

    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
     
    Steve Holden, Jan 11, 2005
    #6
  7. Steve Holden wrote:
    > vincent wehren wrote:
    >
    >> rbt wrote:
    >>
    >>> If I have a Python list that I'm iterating over and one of the
    >>> objects in the list raises an exception and I have code like this:
    >>>
    >>> try:
    >>> do something to object in list
    >>> except Exception:
    >>> pass
    >>>
    >>> Does the code just skip the bad object and continue with the other
    >>> objects in the list, or does it stop?
    >>>
    >>> Thanks

    >>
    >>
    >>
    >> Fire up a shell and try:
    >>
    >> >>> seq = ["1", "2", "a", "4", "5", 6.0]
    >> >>> for elem in seq:

    >> .... try:
    >> .... print int(elem)
    >> .... except ValueError:
    >> .... pass
    >>
    >>
    >> and see what happens...
    >>
    >> --
    >> Vincent Wehren

    >
    >
    > I suspect the more recent versions of Python allow a much more elegant
    > solution. I can't remember precisely when we were allowed to use
    > continue in an except suite, but I know we couldn't in Python 2.1.
    >
    > Nowadays you can write:
    >
    > Python 2.4 (#1, Dec 4 2004, 20:10:33)
    > [GCC 3.3.3 (cygwin special)] on cygwin
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> for i in [1, 2, 3]:

    > ... try:
    > ... print i
    > ... if i == 2: raise AttributeError, "Bugger!"
    > ... except AttributeError:
    > ... print "Caught exception"
    > ... continue
    > ...
    > 1
    > 2
    > Caught exception
    > 3
    > >>>

    >
    > To terminate the loop on the exception you would use "break" instead of
    > "continue".


    What do you mean by a more elegant solution to the problem? I thought
    the question was if a well-handled exception would allow the iteration
    to continue with the next object or that it would stop. Why would you
    want to use the continue statement when in the above case that is
    obviously unnecessary?:

    $ python
    Python 2.4 (#1, Dec 4 2004, 20:10:33)
    [GCC 3.3.3 (cygwin special)] on cygwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> for i in [1,2,3]:

    .... try:
    .... if i == 2: raise AttributeError, "Darn!"
    .... except AttributeError:
    .... print "Caught Exception"
    ....
    1
    2
    Caught Exception
    3
    >>>


    Or do you mean that using "continue" is more elegant than using "pass"
    if there are no other statements in the except block?


    Regards,
    --
    Vincent Wehren
    >
    > regards
    > Steve
     
    vincent wehren, Jan 11, 2005
    #7
    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. Ahmed Moustafa
    Replies:
    5
    Views:
    30,100
    Chris Smith
    Jul 14, 2004
  2. Gilles Kuhn
    Replies:
    0
    Views:
    451
    Gilles Kuhn
    Sep 15, 2003
  3. Paul Miller
    Replies:
    3
    Views:
    1,049
    Alex Martelli
    Nov 12, 2003
  4. Anjan Bhowmik
    Replies:
    1
    Views:
    500
    Misbah Arefin
    Feb 14, 2008
  5. divya
    Replies:
    1
    Views:
    1,112
    Munna
    May 28, 2008
Loading...

Share This Page