0

When I call the following method, two returns are executed and I can't figure out why.

def Build(self, name = None):
    if self.buildData:
        try:
            installData = self.buildData.Build(name)
            return BuildResult(True, installData)
        except:
            pass
    else:
        Log("Application has no <build> data")
    return BuildResult(False, None)

What happens is this:

  1. The method is called with a valid string, say "abc"
  2. self.buildData.Build(name) is called and BuildResult(True, installData) constructor is run
  3. The last return-statement is also executed
2
  • 2
    Please fix your "except:" clause to print the exception that's actually raised there. That will help you see what's happening. Commented Apr 7, 2011 at 12:05
  • 1
    What is self.buildData? Are you sure it isn't a reference back to the parent class? Commented Apr 7, 2011 at 12:07

2 Answers 2

7

Well, I guess that BuildResult() fails, exception is passed and you go into second return. You can easy test it just by adding some debug print in except - never make exceptions silent :)

Sign up to request clarification or add additional context in comments.

Comments

1

When return BuildResult(True, installData) is executed BuildResult(True, installData) is evaluated first. My guess is, this function raises an exception which is caught and passed. After that return BuildResult(False, None) is executed.

You should see what exception is raised and handle it properly.

I would rewrite the function as follows:

def Build(self, name = None):
    if self.buildData:
        try:
            installData = self.buildData.Build(name)
        except: # TODO: catch only expected exception
            pass
        return BuildResult(True, installData)
    else:
        Log("Application has no <build> data")
    return BuildResult(False, None)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.