Assuming this piece of code:
connection = get_some_connection() # maybe from oursql
with connection.cursor() as cursor:
cursor.execute('some query')
I understand that after finishing cursor.close() will be executed automatically. What about exceptions? Must I put them inside?
connection = get_some_connection() # maybe from oursql
with connection.cursor() as cursor:
try:
cursor.execute('some query')
except IntegrityError, e:
# handle exceoption
Or is there a nicer way to handle them with the with statement?