Handle Error in Python with try except

for i in range(20):
  try:
    something here
  except IOError:
    pass

 When python finds error in the code, it will stop and escape the loop and it might be a nightmare for us, so making an exception handler is a smart choice.

The above code is the simplest usage of try… except function, it means that, from the range(20) we will do ‘something here’, but if there are errors, here for example the error IOError is the error of not finding the object, thanks to try except, python in this case will process the ‘pass’ (forget the error and continue). So the loop is safe and can be continued.

Please follow and like us:
error
The following two tabs change content below.

Hien NGUYEN

Postdoctoral Researcher at University of La Rochelle
Hien Nguyen is a geomechanics researcher who likes to share his passion and skills in data analyzing, programming, arts and lifestyle.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.