python - 继承基本 Python 错误异常对象(语法)
问题描述
在 Visual Basic 中,有一个继承的基础对象可有效用于错误调试目的。Python中的“Err”对象是否有等价物?
Dim Msg As String
' If an error occurs, construct an error message.
On Error Resume Next ' Defer error handling.
Err.Clear
Err.Raise(6) ' Generate an "Overflow" error.
' Check for error, then show message.
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & ControlChars.CrLf & Err.Description
MsgBox(Msg, MsgBoxStyle.Information, "Error")
End If
例如:
def exec_sproc(sql,cnxn):
try:
cursor=cnxn.cursor()
cursor.execute(sql)
cnxn.commit()
cursor.close()
except(err):
print("error: {0}".format(err))
有一个建议可以试试
except Exception,e: print str(e)
我得到一个无效的语法,因为 'e' 没有定义,或者在前面的例子中 err 显示为无效的语法。
Python 中的基本错误对象是什么?在大多数示例中,演示的是用户定义的自定义错误,而不是编写返回实际“Python”错误的代码。如果我知道错误是什么,我宁愿不犯。相反,向我展示 Python 认为的错误。(除了无效语法之外的任何东西都会有所帮助)
解决方案
我想这就是你要找的
def exec_sproc(sql,cnxn):
try:
cursor=cnxn.cursor()
cursor.execute(sql)
cnxn.commit()
cursor.close()
except Exception as err:
print("error: {0}".format(err))
有关 Python 异常的更多信息,请看这里
推荐阅读
- javascript - 清除历史状态而不在反应路由器中重新加载
- docker - 为 Druid 构建 Docker 映像时出错
- c++ - 使用 Boost.Spirit 构建数学表达式求值器时出现编译错误
- svg - 使用 Firefox 或 Edge 时子元素在 SVG 中被剪裁,但在 Chrome 中工作正常
- makefile - Windows上的Makefile通配符
- python - 在装饰器类的实例化范围之外应用函数装饰器?
- node.js - 邀请未注册用户使用我的应用程序的最安全方法是什么?
- swift - 在 GitHub Actions Ubuntu 中安装 Swift 时遇到问题
- .htaccess - 在 htaccess 中重定向到文件夹和 https
- python - 我需要 python 脚本来选择所有 AVI 文件