NameError, global not defined when using try,except

让人想犯罪 __ 提交于 2019-12-12 01:29:21

问题


Edit:
Ignore this, I figured it out about 3 seconds after posting this but can't delete it =(

I have this try, except code for working with RackSpace cloudfiles

    try:
        cacheobject = cachecontainer.get_object('file.jpg')
    except NoSuchObject as objectname:
        raise tornado.web.HTTPError(404)

If 'file.jpg' is not found, the exception 'NoSuchObject' is raised. When I run this code I get the error

except NoSuchObject as objectname:
NameError: global name 'NoSuchObject' is not defined

I tried putting NoSuchObject in quotes but then I got an error about string exceptions being depreciated.


回答1:


facepalm

Yeah this programming thing's only my job, nothing big

I have to use the stupid thingy thing don't I

try:
    cacheobject = cachecontainer.get_object('file.jpg')
except cloudfiles.errors.NoSuchObject as objectname:
    raise tornado.web.HTTPError(404)

I'll try and not be stupid in future




回答2:


NoSuchObject is in another module, probably in cachecontainer. You have to import it.




回答3:


did you try with an explicit import ? like this :

from cloudfiles.errors import NoSuchObject


来源:https://stackoverflow.com/questions/6060865/nameerror-global-not-defined-when-using-try-except

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!