On Error语句清除VbScript错误对象

时间:2016-11-11 16:57:28

标签: vbscript error-handling

有点像VbScript的新手,并尝试实现错误处理。我的方法是将错误对象传递给HandleErr子,但错误显然是通过带有sub的“On Error Resume Next”语句清除的。使用Windows 7。

Exception

我想有一个简单的答案。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您希望在到达On Error Resume Next后停止HandleErr()的跳过错误。同时使用Err.Clear()重置Err对象。

On Error Resume Next
Dim x
x = 1/0
MsgBox "Original Error: " & Err.Number & " - " & Err.Description
if Err.Number <> 0 then HandleErr Err
'Stop skipping lines when errors occur.
On Error Goto 0

Sub HandleErr(objErr)
    MsgBox "Error in HandleErr: " & objErr.Number & " - " & objErr.Description '### objErr.Number becomes zero.
    'Clear current error now you have trapped it.
    Err.Clear
    WScript.Quit objErr.Number
End Sub

就个人而言,虽然我不会将Err传递给您的函数,因为Err是一个全局内置对象,因此您仍然可以检查这些值而不将其传入。

On Error Resume Next
Dim x
x = 1/0
MsgBox "Original Error: " & Err.Number & " - " & Err.Description
Call HandleErr()
'Stop skipping lines when errors occur.
On Error Goto 0

Sub HandleErr()
    'Do we need to trap an error?
    If Err.Number <> 0 Then
        MsgBox "Error in HandleErr: " & Err.Number & " - " & Err.Description '### Err.Number becomes zero.
        'Clear current error now you have trapped it.
        Err.Clear
        WScript.Quit Err.Number
    End If
End Sub