Search code examples
python-2.7try-catchcontinue

Using multiple try: except to handle error does not work


I have a list of files I am iterating through:

 condition = True
 list = ['file1', 'file2', 'file3']
   for item in list:
     if condition == True      
        union = <insert process>
      ....a bunch of other stuff.....

Let's say the code works fine on file1 and file3, but when it gets to file2, an IO error gets thrown. What I want to do is route around file2 when the IOError is thrown a go to back to the next item in the list. I want to use a try: except method to do this but I can't seem to get it right. Note: I have an overall try-catch at the beginning of the code. I am not sure if it may interfere with having a second one on just a specific section of the code.

try:
    try:
      condition = True
      list = ['file1', 'file2', 'file3']
      for item in list:
        if condition == True      
          union = <insert process>
      ....a bunch of other stuff.....

    except IOError:
      continue
    .....a bunch more stuff.....
except Exception as e:
    logfile.write(e.message)
    logfile.close()
    exit()

What is the difference between 'pass' and 'continue' and is why would the above code not work? Do I need to add more specific information to the IOError part?


Solution

  • What's the difference between pass and continue ?

    pass is a no-op, it tells python to simply do nothing and go to the next instruction.

    continue is a loop operation, it tells python to ignore whatever else code is left in this iteration of the loop and simply go to the next iteration as if it had reached the end of the loop block.

    For example:

    def foo():
        for i in range(10):
            if i == 5:
               pass
            print(i)
    
    def bar():
        for i in range(10):
            if i == 5:
               continue
            print(i)
    

    The first will print 0,1,2,3,4,5,6,7,8,9, but the second will print 0,1,2,3,4,6,7,8,9 because the continue statement will cause python to jump back to the start and not go on to the print instruction, whereas pass will continue executing the loop normally.

    Why would the above code not work?

    The issue with your code is that the try block is outside the loop, once an exception occurs inside the loop, the loop terminates at that point and jumps to the except block outside the loop. To fix that, just move the try and except blocks into your for loop:

    try:
      condition = True
      list = ['file1', 'file2', 'file3']
      for item in list:
         try:
            # open the file 'item' somewhere here
            if condition == True      
                union = <insert process>
            ....a bunch of other stuff.....
    
         except IOError:
             # this will now jump back to for item in list: and go to the next item
             continue
        .....a bunch more stuff.....
    except Exception as e:
       logfile.write(e.message)
       logfile.close()
       exit()