简体   繁体   中英

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

  • 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)

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM