【发布时间】:2011-08-13 20:03:23
【问题描述】:
假设这段代码:
connection = get_some_connection() # maybe from oursql
with connection.cursor() as cursor:
cursor.execute('some query')
我了解cursor.close() 完成后会自动执行。例外情况如何?我必须把它们放在里面吗?
connection = get_some_connection() # maybe from oursql
with connection.cursor() as cursor:
try:
cursor.execute('some query')
except IntegrityError, e:
# handle exceoption
或者有没有更好的方法来使用 with 语句来处理它们?
【问题讨论】:
标签: python exception-handling with-statement