473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

exceptions and items in a list

rbt
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
Jul 18 '05 #1
6 1269
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
Jul 18 '05 #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?

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
Jul 18 '05 #3
rbt
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.
Jul 18 '05 #4
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
Jul 18 '05 #5
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
Jul 18 '05 #6
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

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ahsan Imam | last post by:
Hello All, I am trying to move an application from python 1.5.2 to 2.3. The code works fine in 1.5.2 but gives the exception (exceptions.TypeError unpack non-sequence) in python 2.3. I did not...
33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
24
by: mag31 | last post by:
Is there any way to find out if a particular .net function will throw an exception without first generating the exception? I am using structured exception handling i.e. try catch finally blocks...
15
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions...
22
by: Drew | last post by:
How do I know which exceptions are thrown by certain methods? For example, reading a file might throw an IO Exception, etc. In Java, the compiler won't even let you compile unless you put your...
3
by: Julia | last post by:
Thanks for all you responses assuming I have a collection of objects which I want to save in a database I wonder if the following is the way to deal with a situation that only some of the...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.