Ignoring an error message to continue with the loop in python [duplicate]

一曲冷凌霜 提交于 2019-12-20 10:58:28

问题


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.

Is it possible in Python to bypass the error and continue with the other iterations?

The error message is

#* 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.

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)

Is this doable? Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/38707513/ignoring-an-error-message-to-continue-with-the-loop-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!