0

Hello I am trying to make 'try' only work under one condition:

try:
    print "Downloading URL: ", url
    contents = urllib2.urlopen(url).read()
except:
    message = "No record retrieved."
    print message
    return None

I do not want the above code to work if the kwarg nodownload is True.

So I have tried the following:

try:
    if nodownload:
        print "Not downloading file!"
        time.sleep(6)
        raise
    print "Downloading URL: ", url
    contents = urllib2.urlopen(url).read()
except:
    message = "No record retrieved."
    print message
    return None

The above always downloads no matter if the --nd argument is passed in the command line. The below always skips the file rather the argument is passed or not.

    if not nodownload:
        print "Not downloading file!"
        time.sleep(6)
        raise
    print "Downloading URL: ", url
    contents = urllib2.urlopen(url).read()
except:
    message = "No record retrieved."
    print message
    return None

No download is input at the command line:

parser.add_argument('--nodownload', dest='nodownload', action='store_true',
                    help='This doesn't work for some reason')
6
  • 1
    if not nodownload: ? Commented Apr 12, 2016 at 16:29
  • I tried something along those lines and it failed. I will post updated code after lunch. Thanks. Commented Apr 12, 2016 at 16:30
  • Please make a minimal reproducible example. It is not clear where the nodownload variable is from. Commented Apr 12, 2016 at 16:31
  • where is nodownload?? Commented Apr 12, 2016 at 16:34
  • 1
    Something like adding a if 'nodownload' in kwargs and not kwargs['nodownload']: raise MyException in the try block...? Or put the try block inside a if 'nodownload' not in kwargs or not kwargs['nodownload']:. Anyway you should take care of bare except clauses, they may hide you exceptions you might want to know about. Commented Apr 12, 2016 at 16:37

2 Answers 2

1

You can use raise to cause an exception when you need to, thus making the try fail.

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

Comments

0

As others have mentioned, one can raise an exception.

Apart from using predefined exceptions, you may also use your own:

class BadFriend(Exception):
    pass


class VirtualFriend(Exception):
    pass


class DeadFriend(Exception):
    pass


try:
    name = raw_input("Tell me name of your friend: ")
    if name in ["Elvis"]:
        raise DeadFriend()
    if name in ["Drunkie", "Monkey"]:
        raise BadFriend()
    if name in ["ET"]:
        raise VirtualFriend()
    print("It is nice, you have such a good friend.")
except BadFriend:
    print("Better avoid bad friends.")
except VirtualFriend:
    print("Whend did you shake hands with him last time?")
except DeadFriend:
    print("I am very sorry to tell you, that...")

You can even pass some data via raised exception, but be careful not to abuse it too far (if standard structures are working, use simpler ones).

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.