VBScript 的错误处理:不抛出错误
Error handling on VBScript: Not throwing error
如果发生错误,它将转入 DisplayCustomError 子程序,但不会抛出异常。
预期结果 - 在 objHTTP.send (json) 之后它应该抛出异常消息。我试过 Call Err.Raise 但它没有抛出任何东西。代码在 VBscript
sub run
On Error Resume Next
wrapper.getVariable( "Plant_Price_PerKW" ).value = excel.range( "'Cases'!$H1" )
wrapper.getVariable( "Net_Present_Value" ).value = excel.range( "'Cases'!$H2" )
wrapper.getVariable( "IRR" ).value = excel.range( "'Cases'!$H3" )
Dim strMessage
If Err.Number <> 0 And excel.range( "'Cases'!$H3" ) = "" Then
strMessage = "IRR cannot be computed. "
DisplayCustomError(strMessage)
WScript.Quit
ElseIf Err.Number <> 0 And (excel.range( "'Cases'!$H3" ) <> "") Then
strMessage = "Other Outputs cannot be computed."
DisplayCustomError(strMessage)
WScript.Quit
End If
end sub
Sub DisplayCustomError(strMessage)
If Err.Number <> 0 Then
Dim errorMessage, objHTTP, URL, json, errorCode, uniqueId, networkInfo, jobId
errorMessage ="Error while executing EVMLite. Error number " & Err.Number & ". " & Err.Description & " " & Err.Source & strMessage
errorCode = "ERR-1004"
uniqueId = wrapper.getVariable("UniqueId").value
Set networkInfo = CreateObject("WScript.NetWork")
jobId = networkInfo.ComputerName
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "http://10.93.244.224343:9005/vpp/logerror"
objHTTP.Open "POST", URL, False
objHTTP.SetRequestHeader "Content-Type", "application/json"
json = "{""jobId"": """& jobId &""", ""uniqueId"": """& uniqueId &""", ""errorCode"": """& errorCode &""", ""errorMessage"": """& errorMessage &"""}"
'MsgBox json
objHTTP.send (json)
On Error Goto 0
Call Err.Raise(vbObjectError + 10, "EVM Failed to execute", errorMessage)
'MsgBox objHTTP.ResponseText
End If
end sub
VBScript 实现 On Error
的有趣之处在于它不是全局的 - 它实际上受范围限制。因此,如果您在 Sub 或 Function 中应用 On Error Resume Next
,然后输入一个新的 Function 或 Sub 将其关闭,则该设置会在 Sub 或 Function 退出时恢复。
在你的例子中,你的 Sub run
设置 On Error Resume Next
然后它调用 Sub DisplayCustomError
设置 On Error GoTo 0
。这只适用于 当你还在里面时 DisplayCustomError
。当它由于 Err.Raise
而退出时,您的 Sub run
将继续,因为在那个范围内,它仍然设置为 On Error Resume Next
.
在调用 DisplayCustomError
之前,您需要在 Sub run
中明确声明 On Error GoTo 0
这是您可以测试的示例 VBS 文件。如果你 运行 这个,什么都不会抛出,程序会显示消息 "Done"。如果您取消注释 Sub1
中的行,则会引发错误 "Example 2".
Sub Sub1()
On Error Resume Next
Err.Raise 1, "Example 1"
'On Error Goto 0
Sub2
End Sub
Sub Sub2()
On Error Goto 0
Err.Raise 2, "Example 2"
End Sub
Sub1
WScript.Echo "Done"
如果发生错误,它将转入 DisplayCustomError 子程序,但不会抛出异常。 预期结果 - 在 objHTTP.send (json) 之后它应该抛出异常消息。我试过 Call Err.Raise 但它没有抛出任何东西。代码在 VBscript
sub run
On Error Resume Next
wrapper.getVariable( "Plant_Price_PerKW" ).value = excel.range( "'Cases'!$H1" )
wrapper.getVariable( "Net_Present_Value" ).value = excel.range( "'Cases'!$H2" )
wrapper.getVariable( "IRR" ).value = excel.range( "'Cases'!$H3" )
Dim strMessage
If Err.Number <> 0 And excel.range( "'Cases'!$H3" ) = "" Then
strMessage = "IRR cannot be computed. "
DisplayCustomError(strMessage)
WScript.Quit
ElseIf Err.Number <> 0 And (excel.range( "'Cases'!$H3" ) <> "") Then
strMessage = "Other Outputs cannot be computed."
DisplayCustomError(strMessage)
WScript.Quit
End If
end sub
Sub DisplayCustomError(strMessage)
If Err.Number <> 0 Then
Dim errorMessage, objHTTP, URL, json, errorCode, uniqueId, networkInfo, jobId
errorMessage ="Error while executing EVMLite. Error number " & Err.Number & ". " & Err.Description & " " & Err.Source & strMessage
errorCode = "ERR-1004"
uniqueId = wrapper.getVariable("UniqueId").value
Set networkInfo = CreateObject("WScript.NetWork")
jobId = networkInfo.ComputerName
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "http://10.93.244.224343:9005/vpp/logerror"
objHTTP.Open "POST", URL, False
objHTTP.SetRequestHeader "Content-Type", "application/json"
json = "{""jobId"": """& jobId &""", ""uniqueId"": """& uniqueId &""", ""errorCode"": """& errorCode &""", ""errorMessage"": """& errorMessage &"""}"
'MsgBox json
objHTTP.send (json)
On Error Goto 0
Call Err.Raise(vbObjectError + 10, "EVM Failed to execute", errorMessage)
'MsgBox objHTTP.ResponseText
End If
end sub
VBScript 实现 On Error
的有趣之处在于它不是全局的 - 它实际上受范围限制。因此,如果您在 Sub 或 Function 中应用 On Error Resume Next
,然后输入一个新的 Function 或 Sub 将其关闭,则该设置会在 Sub 或 Function 退出时恢复。
在你的例子中,你的 Sub run
设置 On Error Resume Next
然后它调用 Sub DisplayCustomError
设置 On Error GoTo 0
。这只适用于 当你还在里面时 DisplayCustomError
。当它由于 Err.Raise
而退出时,您的 Sub run
将继续,因为在那个范围内,它仍然设置为 On Error Resume Next
.
在调用 DisplayCustomError
Sub run
中明确声明 On Error GoTo 0
这是您可以测试的示例 VBS 文件。如果你 运行 这个,什么都不会抛出,程序会显示消息 "Done"。如果您取消注释 Sub1
中的行,则会引发错误 "Example 2".
Sub Sub1()
On Error Resume Next
Err.Raise 1, "Example 1"
'On Error Goto 0
Sub2
End Sub
Sub Sub2()
On Error Goto 0
Err.Raise 2, "Example 2"
End Sub
Sub1
WScript.Echo "Done"