Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误异常应继承基本Python错误对象_Python_Vba_Error Handling - Fatal编程技术网

错误异常应继承基本Python错误对象

错误异常应继承基本Python错误对象,python,vba,error-handling,Python,Vba,Error Handling,在VisualBasic中,有一个继承的基本对象对于错误调试是有效的。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. '

在VisualBasic中,有一个继承的基本对象对于错误调试是有效的。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 
有一个建议可以试试

   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异常的更多信息,请查看

您可以使用
expect
和错误名称,如本例中所示
expect Exception:
在错误累积时绕过错误或打印某些内容。这就是您要寻找的吗?至于建议,您将得到一个错误,因为没有名为
e
的变量/函数执行
print(“e”)
,它会起作用,而且
expect
函数中也有一个输入错误,它应该是
expect Exception as err:print(f“有一个{err}错误”)
。语法错误是因为
除了异常,e:
是Python 2语法。使用
作为
而不是逗号。
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))