错误异常应继承基本Python错误对象 [英] Error Exception Should Inherit Base Python Error Object
问题描述
在Visual Basic中,有一个继承的基础对象,可有效用于错误调试.是否有与"Err"相对应的内容?Python中的对象?
In Visual Basic, there is an inherited base object that is effective for error debugging purposes. Is there an equivalent for the "Err" object in Python?
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显示为无效的语法.
I get an invalid syntax because 'e' is not defined or in the former example err shows as invalid syntax.
Python中的基本错误对象是什么?在大多数示例中,展示了用户定义的自定义错误,而不是对实际的"Python"返回代码进行编码.错误.如果我知道错误是什么,我不希望这样做.相反,请告诉我Python视为错误.(除无效语法外的任何方法都可以帮助您)
What is the base error object in Python? In most examples, user-defined custom errors are demonstrated rather than coding a return of the actual "Python" error. If I know what the error is, I prefer not to make it. Instead, show me what Python sees as the error. (anything other than invalid syntax would help)
推荐答案
我想这就是您要寻找的
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异常的更多信息,请在此处
for more on Python exceptions look here
这篇关于错误异常应继承基本Python错误对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!