praks5432
praks5432

Reputation: 7792

Django Try Except Not Working

I have this code,

    try:
        print "what"
        newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact =         nameAndNumberStore[0])
        print newClassName  
    except:
        print "HAHA"

This always prints "HAHA", despite the fact that I've run the newClassName = ... code in console and it's worked.

Why is this happening?

EDIT

def newGetAllInformation(searchTerm):
nameAndNumberStore = modifySearchTerm(searchTerm)
urlStore = modifyUrl(nameAndNumberStore) # need to make the change here -- why not I go to the site, check for Course name - if that is not there switch, if it is  then scrape 
soup = getHtml(urlStore) 
storeOfBooks = []
storeOfBooks = scrape(soup,nameAndNumberStore)
print nameAndNumberStore[0]
try:
    newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = nameAndNumberStore[0])
    nameAndNumberStore = modifySearchTerm(newClassName.departmentName + " " + nameAndNumberStore[1])
    urlStore = modifyUrl(nameAndNumberStore)
    soup = getHtml(urlStore)
    storeOfBooks = scrape(soup,nameAndNumberStore)

except:
    print "HAHA"

return storeOfBooks

EDIT After further investigation - that is, entering a valid code manually (which worked), I think that there's something up with taking the code from the array- despite the fact that both are the same data type (string).

so newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = "econ") works from file, but newClassName = CourseNameAndCodeAssocition.objects.get(departmentCode__iexact = nameAndNumberStore[0]),where nameAndNumberStore[0] holds econ

Upvotes: 3

Views: 30945

Answers (2)

patrys
patrys

Reputation: 2769

Change it to:

except CourseNameAndCodeAssociation.DoesNotExist:

Each model you create gets its own DoesNotExist exception that extends core ObjectDoesNotExist exception.

Also the best approach is to only use tryexcept around the precise line that you expect to fail. A more pythonic way to write what you have there would be:

department_code = name_and_number_store[0]
class_names = CourseNameAndCodeAssociation.objects.all()
try:
    new_class_name = class_names.get(departmentCode__iexact=department_code)
except CourseNameAndCodeAssociation.DoesNotExist:
    print "HAHA"
else:
    search_term = u'%s %s' % (new_class_name.departmentName,
                              name_and_number_store[1])
    name_and_number_store = modify_search_term(search_term)
    url_store = modify_url(name_and_number_store)
    soup = get_html(url_store)
    store_of_books = scrape(soup, name_and_number_store)

Please also note that the convention in Python is to use lowercase_underscored_names for variables, attributes and function names and CamelCaseNames for class names (instance names are either variables or attributes).

Upvotes: 2

Michael
Michael

Reputation: 741

Please modify the code to this, run it and tell us what exception you are getting:

try:
    print "what"
    newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact =         nameAndNumberStore[0])
    print newClassName  
except Exception as e:
    print "HAHA"
    print e

Also, it would probably help to have a debugger installed on your box. I can recommend Eclipse in combination with PyDev, but that is a personal choice. There are lots of great options out there.

Eclipse IDE - download the basic Java version of 120MB

then install this plugin on top of it - Pydev

Upvotes: 10

Related Questions