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

守給你的承諾、 提交于 2020-01-02 07:24:10

问题


  • I'd like to avoid writting errorCount += 1 in more than one place.
  • I'm looking for a better way than
    success = False
    try:
        ...
    else:
        success = True
    finally:
        if success:
            storage.store.commit()
        else:
            storage.store.rollback()
  • I'm trying to avoid store.rollback() in every except clause.

Any idea on how to do this?

count = 0
successCount = 0
errorCount = 0
for row in rows:
    success = False
    count += 1
    newOrder = storage.RepeatedOrder()
    storage.store.add(newOrder)
    try:
        try:
            newOrder.customer = customers[row.customer_id]
        except KeyError:
            raise CustomerNotFoundError, (row.customer_id,)
        newOrder.nextDate = dates[row.weekday]
        _fillOrder(newOrder, row.id)
    except CustomerNotFoundError as e:
        errorCount += 1
        print u"Error: Customer not found. order_id: {0}, customer_id: {1}".format(row.id, e.id)
    except ProductNotFoundError as e:
        errorCount += 1
        print u"Error: Product not found. order_id: {0}, product_id: {1}".format(row.id, e.id)
    else:
        success = True
        successCount += 1
    finally:
        if success:
            storage.store.commit()
        else:
            storage.store.rollback()
print u"{0} of {1} repeated orders imported. {2} error(s).".format(successCount, count, errorCount)

回答1:


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




回答2:


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




回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/877440/try-except-except-how-to-avoid-repeating-code

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