Program Club

VBScript — 오류 처리 사용

proclub 2020. 10. 9. 12:35
반응형

VBScript — 오류 처리 사용


VBScript를 사용하여 오류를 포착하고 기록 (즉, "log something"오류시) 한 다음 스크립트의 다음 줄을 다시 시작하고 싶습니다.

예를 들면

오류시 다음 재개
'1 단계 수행
'2 단계 수행
'3 단계 수행

1 단계에서 오류가 발생하면 해당 오류를 기록하거나 다른 사용자 지정 기능을 수행 한 다음 2 단계에서 다시 시작합니다. 가능합니까? 어떻게 구현할 수 있습니까?

편집 : 이런 식으로 할 수 있습니까?

오류시 myErrCatch 재개
'1 단계 수행
'2 단계 수행
'3 단계 수행

myErrCatch :
'로그 오류
다음 재개

VBScript에는 예외를 던지거나 잡는 개념이 없지만 런타임은 마지막으로 수행 된 작업의 결과를 포함하는 전역 Err 개체를 제공합니다. 각 작업 후에 Err.Number 속성이 0이 아닌지 명시 적으로 확인해야합니다.

On Error Resume Next

DoStep1

If Err.Number <> 0 Then
  WScript.Echo "Error in DoStep1: " & Err.Description
  Err.Clear
End If

DoStep2

If Err.Number <> 0 Then
  WScript.Echo "Error in DoStop2:" & Err.Description
  Err.Clear
End If

'If you no longer want to continue following an error after that block's completed,
'call this.
On Error Goto 0

"On Error Goto [label]"구문은 Visual Basic 및 VBA (Visual Basic for Applications)에서 지원되지만 VBScript는이 언어 기능을 지원하지 않으므로 위에서 설명한대로 On Error Resume Next를 사용해야합니다.


참고 On Error Resume Next세계적으로 설정되어 있지 않습니다. 예를 들어 코드의 안전하지 않은 부분을 오류가 발생하면 즉시 중단되는 함수에 넣을 수 있으며, 선례가 포함 된 sub에서이 함수를 호출 할 수 있습니다 OERN.

ErrCatch()

Sub ErrCatch()
    Dim Res, CurrentStep

    On Error Resume Next

    Res = UnSafeCode(20, CurrentStep)
    MsgBox "ErrStep " & CurrentStep & vbCrLf & Err.Description

End Sub

Function UnSafeCode(Arg, ErrStep)

    ErrStep = 1
    UnSafeCode = 1 / (Arg - 10)

    ErrStep = 2
    UnSafeCode = 1 / (Arg - 20)

    ErrStep = 3
    UnSafeCode = 1 / (Arg - 30)

    ErrStep = 0
End Function

나는 예외적으로 VBScript를 처음 접했기 때문에 이것이 모범 사례로 간주되지 않거나 내가 아직 알지 못하는 방식으로 이렇게하지 말아야 할 이유가있을 수 있지만 이것이 내가 트리밍하기 위해 생각 해낸 해결책입니다. 내 메인 코드 블록에서 오류 로깅 코드의 양을 줄였습니다.

Dim oConn, connStr
Set oConn = Server.CreateObject("ADODB.Connection")
connStr = "Provider=SQLOLEDB;Server=XX;UID=XX;PWD=XX;Databse=XX"

ON ERROR RESUME NEXT

oConn.Open connStr
If err.Number <> 0 Then : showError() : End If


Sub ShowError()

    'You could write the error details to the console...
    errDetail = "<script>" & _
    "console.log('Description: " & err.Description & "');" & _
    "console.log('Error number: " & err.Number & "');" & _
    "console.log('Error source: " & err.Source & "');" & _
    "</script>"

    Response.Write(errDetail)       

    '...you could display the error info directly in the page...
    Response.Write("Error Description: " & err.Description)
    Response.Write("Error Source: " & err.Source)
    Response.Write("Error Number: " & err.Number)

    '...or you could execute additional code when an error is thrown...
    'Insert error handling code here

    err.clear
End Sub

파사드 함수에서 단계 함수 호출을 다시 그룹화 할 수 있습니다.

sub facade()
    call step1()
    call step2()
    call step3()
    call step4()
    call step5()
end sub

Then, let your error handling be in an upper function that calls the facade :

sub main()
    On error resume next

    call facade()

    If Err.Number <> 0 Then
        ' MsgBox or whatever. You may want to display or log your error there
        msgbox Err.Description
        Err.Clear
    End If

    On Error Goto 0
end sub

Now, let's suppose step3() raises an error. Since facade() doesn't handle errors (there is no On error resume next in facade()), the error will be returned to main() and step4() and step5() won't be executed.

Your error handling is now refactored in 1 code block

참고URL : https://stackoverflow.com/questions/157747/vbscript-using-error-handling

반응형