Ignoring an error message to continue with the loop in python

Question:

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!

Asked By: rcty

||

Answers:

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
Answered By: Oleg Sklyar