[英]Continue loop after exception is excepted
I have a loop with a try...except
: 我有一个
try...except
的循环try...except
:
try:
for sqlcommand in sqlcommands:
cursor.execute(sqlcommand)
except psycopg2.ProgrammingError as err:
print "Programming error spotted. Message -->"
print err
This loop creates tables in SQL. 此循环在SQL中创建表。 But I want here is if there is already table(Programming exception is generated), print the message under exception but continue loop, not stop it.
但是我想在这里是如果已经有表格(生成编程异常),在异常下打印消息,但是继续循环,而不是停止它。 Can I do it?
我可以做吗?
You simply need to change the order of try
statement: 您只需要更改
try
语句的顺序即可:
for sqlcommand in sqlcommands:
try:
cursor.execute(sqlcommand)
except psycopg2.ProgrammingError as err:
print "Programming error spotted. Message -->"
print err
In this case while iterating the loop, for each query the try except
block will run and if error is found the main for
loop iteration would still be working. 在这种情况下,在迭代循环时,对于每个查询,将运行
try except
块,并且如果发现错误,则主要的for
循环迭代仍将起作用。
for sqlcommand in sqlcommands:
try:
cursor.execute(sqlcommand)
except psycopg2.ProgrammingError as err:
print "Programming error spotted. Message -->"
print err
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.