草庐IT

exception-derived

全部标签

python - jinja2.exceptions.TemplateNotFound 错误

这个问题在这里已经有了答案:FlaskraisesTemplateNotFounderroreventhoughtemplatefileexists(13个回答)关闭7年前。我使用flask,当我调用此url时出现此错误:/login这是我的登录方法:@app.route('/login')deflogin():ifauthenticateForPanel():returnredirect(url_for("panel"))else:getParam=request.args.getlist('redirect_uri')ifgetParam:ref=getParam[0]else:r

python - 如何修复 : W602 deprecated form of raising exception

如果我使用pylint(通过sublimerlinter),我会收到以下警告消息:W602已弃用的引发异常的形式这是我在代码中使用异常的方式:ifCONDITION==True:raiseValueError,HELPING_EXPLANATION 最佳答案 像这样提出你的异常:ifCONDITION==True:raiseValueError(HELPING_EXPLANATION)来自PEP8--StyleGuideforPythonCode-ProgrammingRecommendations:Whenraisinganexc

python - 如何在 try/except block 中公开变量?

如何在try/exceptblock中公开变量?importurllib.requesttry:url="http://www.google.com"page=urllib.request.urlopen(url)text=page.read().decode('utf8')except(ValueError,RuntimeError,TypeError,NameError):print("Unabletoprocessyourrequestdude!!")print(text)此代码返回错误NameError:name'text'isnotdefined如何使变量文本在try/exc

python - django.core.exceptions.ImproperlyConfigured : Requested setting CACHES, 但未配置设置。您必须定义环境变量

我已经尝试了所有能找到的方法来解决这个问题,但现在我的头发开始掉了一点。我收到此错误:django.core.exceptions.ImproperlyConfigured:RequestedsettingCACHES,butsettingsarenotconfigured.YoumusteitherdefinetheenvironmentvariableDJANGO_SETTINGS_MODULEorcallsettings.configure()beforeaccessingsettings.当我这样做时,我的脚本运行良好:python3./manage.pyrunserver但是

python - 在 Python 中使用 try/except 将字符串转换为 Int

所以我很困惑如何使用try/except函数将字符串转换为int。有谁知道如何做到这一点的简单功能?我觉得我在字符串和整数上仍然有点朦胧。我非常有信心整数与数字有关。字符串...不是那么多。 最佳答案 在使用try/exceptblock时,请务必具体说明您要捕获的异常。string="abcd"try:string_int=int(string)print(string_int)exceptValueError:#Handletheexceptionprint('Pleaseenteraninteger')Try/Excepts非

python - 有没有更 Pythonic 的方式来组合 Else : statement and an Except:?

我有一段代码在AutoCAD中搜索包含某些关键字的文本框(例如,在本例中为"overall_weight")并将其替换为字典中的值。但是,有时字典键被分配给一个空字符串,有时,键完全不存在。在这些情况下,应将"overall_weight"关键字替换为"N/A"。我想知道是否有一种更Pythonic的方式可以将KeyError异常和else结合到nObject.TextString="N/A"所以它不会输入两次。ifnObject.TextString=="overall_weight":try:ifself.var.jobDetails["OverallWeight"]:nObjec

python - TypeError:exceptions 必须是旧式类或派生自 BaseException,而不是 str

以下是我的代码:test='abc'ifTrue:raisetest+'def'当我运行它时,它给了我TypeErrorTypeError:exceptionsmustbeold-styleclassesorderivedfromBaseException,notstr那么test应该是什么样的类型呢? 最佳答案 raise的唯一参数表示要引发的异常。这必须是异常实例或异常类(派生自Exception的类)。试试这个:test='abc'ifTrue:raiseException(test+'def')

python - 在 python3.3 中导入 docx 时出现错误 ImportError : No module named 'exceptions'

当我导入docx我有这个错误:File"/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/docx-0.2.4-py3.3.egg/docx.py",line30,infromexceptionsimportPendingDeprecationWarningImportError:Nomodulenamed'exceptions'如何解决这个错误(python3.3,docx0.2.4)? 最佳答案 如果您使用的是python3

Python urllib2 : Reading content body even during HTTPError exception?

我正在使用urllib2通过HTTP获取页面。有时,当我的请求包含错误时,资源会引发HTTP错误400(错误请求)。但是,该响应还包含一个提供详细错误消息的XML元素。能够看到该错误而不仅仅是urllib2返回的HTTPError异常会非常方便。如何在异常情况下返回文档内容? 最佳答案 importurllib2try:request=urllib2.Request('http://www.somesite.com')response=urllib2.urlopen(req)excepturllib2.HTTPErrorase:er

python - Return 语句的奇怪 Try-Except-Else-Finally 行为

这是一些行为异常的代码。这是我编写的行为的简化版本。这仍然会证明奇怪的行为,我对为什么会发生这种情况有一些具体的问题。我在Windows7上使用Python2.6.6。defdemo1():try:raiseRuntimeError,"ToForceIssue"except:return1else:return2finally:return3defdemo2():try:try:raiseRuntimeError,"ToForceIssue"except:return1else:return2finally:return3except:print4else:print5finally: