忽略错误消息以继续 python 中的循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38707513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:19:22  来源:igfitidea点击:

Ignoring an error message to continue with the loop in python

pythonloopserror-handlingabaqus

提问by rcty

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 脚本在 Abaqus 中执行一些函数。现在,在运行了一些迭代之后,Abaqus 由于错误而退出脚本。

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

在 Python 中是否有可能绕过错误并继续其他迭代?

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 循环是给定的;

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!

这是可行的吗?谢谢!

回答by Oleg Sklyar

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