0

I've been reading that you shouldn't just use 'pass' when using try/except in python. I have a couple of situations where I use it in:

source = os.listdir("../myprogdir") # directory where original configs are located
destination = '//' + servername + r'/c$/remotedir/' # destination server directory
for files in source:
    if files.endswith("myconfig.exe.config"):
        try:
            os.makedirs(destination, exist_ok=True)
            shutil.copy(files,destination)
        except:
            pass

and

  source = r'//' + servername + '/c$/remotedir/'
  dest = r"../myprogdir"
  file = "myconfig.exe.config"
  if os.path.isfile(os.path.join(source, file)):  # isfile checks if filename already exists on remote computer
      try:
          shutil.copyfile(os.path.join(source, file), os.path.join(dest, file))

      except:
          print (" Local directory you are copying to does not exist.")
  else:
      print (" ...filename does not exist...")

The thing is, I am using these in functions where I run through multiple computers in a list that I have. If I throw an error such as a computer being down/turned off or a directory/filename does not exist, I just want to skip over it into the next computer name on my list.

So is it ok to use pass in this situation? I guess something could happen if the user running my program does not have permission to perform some of the functions.

What situations would I need to use proper exception handling?

How can I write out a permissions exception error? That is the only thing that might cause a problem with the program. Even though the only people running the program should have proper permissions anyways.

Prox
  • 699
  • 5
  • 11
  • 33
  • 3
    Never use a bare `except:`. If there's a specific type of exception you're expecting, catch that specific type of exception. This isn't Pokemon; don't try to catch 'em all. – user2357112 Jun 22 '17 at 22:13
  • 2
    It would be a lot better of you caught the *explicit exceptions*. As it stands, *any* error will pass silently. *Errors should never pass silently. Unless explicitly silenced...* – juanpa.arrivillaga Jun 22 '17 at 22:15
  • I think [this](https://stackoverflow.com/questions/21553327/why-is-except-pass-a-bad-programming-practice) may be a duplicate. If people agree, I will close. – juanpa.arrivillaga Jun 22 '17 at 22:18
  • So if I don't care if a file/directory doesn't exist and don't care if the computer I am connecting to doesnt exist or is turned off, what other kinds of errors would I be looking for? General Windows errors? – Prox Jun 22 '17 at 22:28

1 Answers1

0

Don't think of the pass statement as having anything to do with exception handling per se.

The pass statement only says, "this space intentionally left blank". pass simply tells the Python interpreter that nothing happens here, and that's not a syntax error.

There are other cases where pass is useful, outside of exception handling. For instance, maybe you want to define a class that doesn't actually do anything, but that inherits from another class without changing anything:

class Person(object):
    pass

class Man(Person):
    """A manly person"""

class Woman(Person):
    pass

Here, we have 3 classes, none of which have methods beyond those inherited from object.

Note that instead of a pass statement in Man, I put a docstring. This is functionally-equivalent to pass in the context of a class/function/method definition. (You wouldn't, however, be able to replace the pass in an except block by a docstring)

To return to your question, think of the except block as being the code that runs when something hasn't worked out the way you hoped. Here, you are saying pass, which is to say, "Just ignore any errors".

You could also put a simple raise statement instead of the pass, to propagate the exception to the enclosing block, all the way up the call stack. This would be functionally-equivalent to leaving out the try/except block altogether.

You could do much more here, as well. Depending on how you want your application to behave when it encounters a failed attempt to do something useful, you could log a message, break out of the loop, re-queue the action for a later retry, increment an error counter, and so on.

BillyBBone
  • 3,144
  • 3
  • 23
  • 27