忽略错误消息以继续执行python中的循环 [英] Ignoring an error message to continue with the loop in python
问题描述
我正在使用Python脚本在Abaqus中执行某些功能.现在,在运行了一些迭代之后,Abaqus由于错误而退出了脚本.
I am using a Python script for executing some function in Abaqus. Now, after running for some iterations Abaqus is exiting the script due to an error.
在Python中是否可以绕过错误并继续进行其他迭代?
Is it possible in Python to bypass the error and continue with the other iterations?
错误消息是
#* The extrude direction must be approximately orthogonal
#* to the plane containing the edges being extruded.
某些迭代会出现错误,我正在寻找一种忽略错误并在遇到此类错误时继续执行循环的方法.
The error comes out for some of the iterations, I am looking for a way to ignore the errors and continue with the loop whenever such error is encountered.
for循环已给出;
The for loop is as given;
for i in xrange(0,960):
p = mdb.models['Model-1'].parts['Part-1']
c = p.cells
pickedCells = c.getSequenceFromMask(mask=('[#1 ]', ), )
e, d1 = p.edges, p.datums
pickedEdges =(e[i], )
p.PartitionCellByExtrudeEdge(line=d1[3], cells=pickedCells, edges=pickedEdges,
sense=REVERSE)
这可行吗?谢谢!
推荐答案
抑制错误或异常而不处理它们通常是一种不好的做法,但是可以很容易地做到这一点:
It is generally a bad practice to suppress errors or exceptions without handling them, but this can be easily done like this:
try:
# block raising an exception
except:
pass # doing nothing on exception
这显然可以在任何其他控制语句中使用,例如循环:
This can obviously be used in any other control statement, such as a loop:
for i in xrange(0,960):
try:
... run your code
except:
pass
这篇关于忽略错误消息以继续执行python中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!