Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 256
rcty Avatar asked Aug 01 '16 20:08

rcty


People also ask

How do you continue a loop if error in Python?

The SyntaxError: continue not properly in loop error is raised when you try to use a continue statement outside of a for loop or a while loop. To fix this error, enclose any continue statements in your code inside a loop.

How do you stop a loop execution in Python?

In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You'll put the break statement within the block of code under your loop statement, usually after a conditional if statement.

How do you continue in case of exception in Python?

When an exception is raised will it continue the code after the try/catch or whatever is outside of the with block? This is equivalent to wrapping your code in a try... catch: pass , so if an exception is raised inside the block, execution will continue after the end of the block.


1 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 
like image 61
Oleg Sklyar Avatar answered Sep 28 '22 02:09

Oleg Sklyar