try… except… except… : how to avoid repeating code

好久不见. 提交于 2019-12-05 18:58:46

This look like a possible application of Python's new with statement. It allows to to unwind operations and release resources securely no matter what outcome a block of code had.

Read about it in PEP 343

My suggestion would to write an logError() method that increments errorCount (make it a member variable) and prints the error. Since your exception code is similar, you could also shorten your code by doing this:

try:
    # something
except (CustomerNotFoundError, ProductNotFoundError), e:
    logError(e)

You can print whatever you want based on e.

Also, you don't need to track succeses: successCount = len(rows) - errorCount

You could simply wrap up your exception implementation inside an exception specific container class, that way you could also avoid all these explicit print calls (which may come in handy once you change your interface, e.g. when supporting a GUI), instead you would have a method like error(msg), which in turn could internally increase the error count accordingly. In other words, simply set up an external helper class that manages your exception handling stuff.

If you like cumulate the errors why you don't cumulate the errors? If you put the error messages on a list the size of the list gives the information you need. You can even postprocess something. You can decide easy if an error occured and print is called only at a single place

Well, according to this page, part 7.4:

http://docs.python.org/reference/compound_stmts.html

This is possible with python ver. >= 2.6. I mean try..except..finally construction.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!