Django Try Except Not Working


Question

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

1
3
10/31/2014 9:47:30 AM

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

9
9/28/2011 8:31:20 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon