Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following are a few basic strategies I regularly use in my more-than-trivial scripts and medium-size applications.</p> <p>Tip 1: Trap the error at every level where it makes sense to continue processing. In your case it may be in the inside the loop. You don't have to protect every single line or every single function call, but only the places where it makes a difference to survive the error.</p> <p>Tip 2: Use the logging module to report what happened in a way that is configurable independently from how you compose the module with other modules in a larger applications. Start importing the root logger in your module, then, using it in a few different places, you may eventually figure out a more sensible logging hierarchy.</p> <pre><code>import logging logger = logging.getLogger() for item in items: try: process(item) except Exception, exc: logger.warn("error while processing item: %s", exc) </code></pre> <p>Tip 3: define an "application exception", eventually you may want to define a hierarchy of such exception but this is better discovered when the need arise. Use such exception(s) to "bubble out" when the data you are dealing with are not what you expected or to signal inconsistent situations, while separating them from the normal standard exception arising from regular bugs or problems outside the modeled domain (IO errors etc).</p> <pre><code>class DomainException(Exception): """Life is not what I expected""" def process(item): # There is no way that this item can be processed, so bail out quickly. # Here you are assuming that your caller will report this error but probably # it will be able to process the other items. if item.foo &gt; item.bar: raise DomainException("bad news") # Everybody knows that every item has more that 10 wickets, so # the following instruction is assumed always being successful. # But even if luck is not on our side, our caller will be able to # cope with this situation and keep on working item.wickets[10] *= 2 </code></pre> <p>The main function is the outmost checkpoint: finally deal here with the possible ways your task finished. If this was not a shell script (but e.g. the processing beneath a dialog in an UI application or an operation after a POST in a web application) only the way you report the error changes (and the use of the logging method completely separates the implementation of the processing from its interface).</p> <pre><code>def main(): try: do_all_the_processing() return 0 except DomainException, exc: logger.error("I couldn't finish. The reason is: %s", exc) return 1 except Exception, exc: logger.error("Unexpected error: %s - %s", exc.__class__.__name__, exc) # In this case you may want to forward a stacktrace to the developers via e-mail return 1 except BaseException: logger.info("user stop") # this deals with a ctrl-c return 1 if __name__ == '__main__': sys.exit(main()) </code></pre>
 

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