frazman
frazman

Reputation: 33243

handling exception in function with return value

If I have a function something like:

def foo(.. ):
  try:
    #something
    return_value = "bleh"
   except Exception,e:
     logging.error("exception " +e)
   return return_value

Does the above look ok? I mean, if I got the exception then return_value is never initialized.

What's a good way to handle exceptions in a function that has some return value?

Upvotes: 1

Views: 1278

Answers (1)

nisargjhaveri
nisargjhaveri

Reputation: 1509

You should return a False or something of that kind to tell the caller that some error has occurred. And then handle this return value in your caller, like if return is False do something else.

def foo(.. ):
    try:
        #something
        return_value = "bleh"
     except Exception,e:
        logging.error("exception " +e)
        return_value = False
     return return_value

Upvotes: 2

Related Questions