Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python already has a very nice construct for doing just this and it doesn't use <code>continue</code>:</p> <pre><code>for i in range(10): try: r = 1.0 / (i % 2) except Exception, e: print(e) else: print(r) </code></pre> <p>I wouldn't nest any more than this, though, or your code will soon get very ugly.</p> <p>In your case I would probably do something more like this as it is far easier to unit test the individual functions and <a href="http://www.python.org/dev/peps/pep-0020/" rel="noreferrer">flat is better than nested</a>:</p> <pre><code>#!/usr/bin/env python def something_that_may_raise(i): return 1.0 / (i % 2) def handle(e): print("Exception: " + str(e)) def do_something_with(result): print("No exception: " + str(result)) def wrap_process(i): try: result = something_that_may_raise(i) except ZeroDivisionError, e: handle(e) except OverflowError, e: handle(e) # Realistically, this will be a different handler... else: do_something_with(result) for i in range(10): wrap_process(i) </code></pre> <p>Remember to always <a href="http://wiki.python.org/moin/HandlingExceptions" rel="noreferrer">catch <em>specific</em> exceptions</a>. If you were not expecting a <em>specific</em> exception to be thrown, it is probably not safe to continue with your processing loop.</p> <h2>Edit following comments:</h2> <p>If you really don't want to handle the exceptions, which I still think is a bad idea, then catch all exceptions (<code>except:</code>) and instead of <code>handle(e)</code>, just <code>pass</code>. At this point <code>wrap_process()</code> will end, skipping the <code>else:</code>-block where the real work is done, and you'll go to the next iteration of your <code>for</code>-loop.</p> <p>Bear in mind, <a href="http://www.python.org/dev/peps/pep-0020/" rel="noreferrer">Errors should never pass silently</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload