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
Answer from Oleg Sklyar on Stack OverflowFor example
Def main(): A = B Print(“Error Test”)
Try: Main() Except exception: Print(“error”)
Instead of showing me an error i instead want it to ignore the error in line 2 and go to Line 3
exception - How to continue python script loop even though IOError? - Stack Overflow
python - How to bypass errors in arcpy for/while loop? - Geographic Information Systems Stack Exchange
python - Ignore exceptions in a loop - Stack Overflow
Python: How to ignore an exception and proceed? - Stack Overflow
The simpler structure is like this:
my_while_or_for_loop:
some_code_here_maybe
try:
my_code_or_function_that_sometimes_fails()
except IOError:
pass # or some code to clean things that went wrong or to log the failure
some_more_code_here_maybe
You want to read the docs
The full construction can be more complex and includes try/except/else/finally.
From an example in docs:
>>> def divide(x, y):
... try:
... result = x / y
... except ZeroDivisionError:
... print "division by zero!"
... else:
... print "result is", result
... finally:
... print "executing finally clause"
Here is is documentationabout exceptions...
Simply, if a code block have possibility to cause some known errors (like input output error) in some conditions, you define an try-exceptblock to handle such errors. That will make your script keep runnung and let you execute diffrent code blocks according to diffrent error status.... Like:
try:
<do something>
except IOError:
<an input-output error occured, do this...>
except ValueError:
<we got something diffrent then we expected, do something diffrent>
except LookupError:
pass # we do not need to handle this one, so just kkeep going...
except:
<some diffrent error occured, do somethnig more diffrent>
If you simply do nothing and continue, you can use pass, like:
try:
<do something>
except:
pass
Try Googling for "python on error resume next" or similar. This returns a number of hits including this one from StackOverflow:
If you know which statements might fail, and how they might fail, then you can use exception handling to specifically clean up the problems which might occur with a particular block of statements before moving on to the next section.
1) An option may be to put a try...except block around the line you suspect will cause the problem, namely the CopyFeatures tool.
2) See also the Python reference on errors, specifically section 8.3. Once you have a reference to "e" you may be able to determine its exception type and handle it as required.
Eg this StackOverflow question contains a similar workflow to yours:
for getter in (get_random_foo, get_random_bar):
try:
return getter()
except IndexError:
continue # Ignore the exception and try the next type.
raise IndexError, "No foos, no bars"
In your case, in place of "IndexError" you'd use whatever you determined the exception type to be for a corrupt shapefile
As Stephen already said you can enclose the CopyFeatures Tool in another try...except Block.
If the tool fails with a specific Shapefile you can log the Tool Message somewhere (I always print it on STDOUT and pipe the outputs to a logfile when i run the script).
What I have to add is: In the Except Block beside the Exception you also have to print the error messages the Tool itself produced. You dont get access to the Tool messages by the Exception (as it should be for sure) but from the arcpy Object by calling
arcpy.getmessages(messageCount - 1)
See http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000000m000000 how to call it and how to get the last messages which are possibly related to the specific Shapefile Error.
After logging this you simply let the script continue with the other shapefiles
except Exception:
pass
Python docs for the pass statement
Generic answer
The standard "nop" in Python is the pass statement:
try:
do_something()
except Exception:
pass
Using except Exception instead of a bare except avoid catching exceptions like SystemExit, KeyboardInterrupt etc.
Python 2
Because of the last thrown exception being remembered in Python 2, some of the objects involved in the exception-throwing statement are being kept live indefinitely (actually, until the next exception). In case this is important for you and (typically) you don't need to remember the last thrown exception, you might want to do the following instead of pass:
try:
do_something()
except Exception:
sys.exc_clear()
This clears the last thrown exception.
Python 3
In Python 3, the variable that holds the exception instance gets deleted on exiting the except block. Even if the variable held a value previously, after entering and exiting the except block it becomes undefined again.
You should change continue for break.
while True:
try:
print(a)
except Exception as e:
print(e)
break
You can read more loop control statements here.
If you want to run in the same loop occurence, Without any continue block you can just add the rest of the code as it is outside the try and except block as follows :
while (condition):
try:
print(a)
except Exception as e:
print(e)
# followed by the rest of code
print("contd")
or if you want to skip the current loop you can use continue.
And this is being repeated because you have set the loop condition to be true, so the loop never ends. To stop the loop you will need a break statement instead of continue.