如果發生錯誤,那麼它將分DisplayCustomError,但它不會拋出異常。 預期結果 - 在objHTTP.send(json)之後,它應該拋出異常消息。我曾嘗試調用Err.Raise,但它不會拋出任何東西。該代碼是用VBScriptVBScript上的錯誤處理:不拋出錯誤
sub run
On Error Resume Next
wrapper.getVariable("Plant_Price_PerKW").value = excel.range("'Cases'!$H$331")
wrapper.getVariable("Net_Present_Value").value = excel.range("'Cases'!$H$782")
wrapper.getVariable("IRR").value = excel.range("'Cases'!$H$783")
Dim strMessage
If Err.Number <> 0 And excel.range("'Cases'!$H$783") = "" Then
strMessage = "IRR cannot be computed. "
DisplayCustomError(strMessage)
WScript.Quit
ElseIf Err.Number <> 0 And (excel.range("'Cases'!$H$783") <> "") 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
您正在使用'On Error Resume Next',它會忽略錯誤。刪除。 – braX
@braX我試圖刪除On Error Resume NextSub,它拋出的異常,但它不會進入子DisplayCustomError(strMessage),當我第二次運行時,它將分DisplayCustomError(strMessage) –
找到一種方法來測試條件導致你正在得到的任何錯誤,然後使用if語句來避免發生錯誤。根據發生的錯誤,編寫代碼是一種不好的方法。 – braX