1

I have this code using a While True with a try catch. The final else statement is always being executed when the 'try' is successful and I'd like to understand why - I think I'm not understanding the program flow correctly.

while True:
    try:
        subprocess.call(["wget", "-O", "splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz", "https://www.splunk.com/bin/splunk/DownloadActivityServlet?architecture=x86_64&platform=linux&version=8.0.1&product=splunk&filename=splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz&wget=true"])
        print("successfully downloaded splunk enterprise")
        time.sleep(2)
    except OSError as e:
        if e.errno == 2:
            print(e)
            print("wget doesn't seem to be installed")
            time.sleep(2)
            print("attempting to install wget")
            time.sleep(2)
            subprocess.call(["yum", "install", "wget"])
        else:
            print(e)
            print("unknown error response, exiting...")
            break
    else:
        print("something else went wrong while trying to download splunk")
        break
5
  • 2
    Fix indentation. Right now it's a syntax error. Commented Jan 22, 2020 at 14:26
  • 1
    while, try, and if all can take an else clause. Indentation determines which one your else clause is actually associated with. With the try statement, it's executed if there were no exceptions. Commented Jan 22, 2020 at 14:27
  • 1
    That's how try-except-else-finally blocks work. You try something. If there's an error, you catch (or 'except' in python) it, otherwise (i.e. "else") do something else. "Finally" you clean up at the end. Commented Jan 22, 2020 at 14:28
  • As an aside, you should be ensuring that wget is installed before you run your code. If it's not installed, just exit; your script shouldn't be installing its own dependencies. For all it knows, wget is already installed, just not in the location your code expects. Commented Jan 22, 2020 at 14:29
  • docs.python.org/3/reference/… Commented Jan 22, 2020 at 14:54

3 Answers 3

1

based on python documentation, try-except can take an optional else statement:

The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.

so based on this, your else statement will run if codes in try doesn't raise any exception!

what you want is another except clause that catches a general exceptation, so you just need to replace else with except:

while True:
    try:
        subprocess.call(["wget", "-O", "splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz", "https://www.splunk.com/bin/splunk/DownloadActivityServlet?architecture=x86_64&platform=linux&version=8.0.1&product=splunk&filename=splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz&wget=true"])
        print("successfully downloaded splunk enterprise")
        time.sleep(2)
    except OSError as e:
        if e.errno == 2:
            print(e)
            print("wget doesn't seem to be installed")
            time.sleep(2)
            print("attempting to install wget")
            time.sleep(2)
            subprocess.call(["yum", "install", "wget"])
        else:
            print(e)
            print("unknown error response, exiting...")
            break
    except:
        print("something else went wrong while trying to download splunk")
        break


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

Comments

0

The else clause of a try executes if the code inside the try did not throw an exception. If you want to catch any exception, use except without specifying an exception class:

except:
    print("something else went wrong while trying to download splunk")
    break

However, consider just omitting this piece of code. As it is currently written, it doesn't tell you what went wrong. If you remove these lines, you will get an error message that tells you what happened and on which line the error occurred.

Comments

0

From here:

The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.

So the else clause will run if the code within the try block runs successfully.

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.