문제

vbscript를 사용하여 오류를 잡고 오류를 기록하고 (예 : 오류 "로그인") 다음 스크립트의 다음 줄을 재개하고 싶습니다.

예를 들어,

On Error Resume Next
'Do Step 1
'Do Step 2
'Do Step 3

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

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

On Error Resume myErrCatch
'Do step 1
'Do step 2
'Do step 3

myErrCatch:
'log error
Resume Next
도움이 되었습니까?

해결책

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 [레이블]구문은 Applications for Applications (VBA)에서 Visual Basic 및 Visual Basic에서 지원되지만 VBScript는이 언어 기능을 지원하지 않으므로 위에서 설명한대로 오류 이력서에 사용해야합니다.

다른 팁

주목하십시오 On Error Resume Next 전 세계적으로 설정되지 않습니다. 코드의 안전하지 않은 부분을 함수에 넣을 수 있습니다. 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

The Steps Functions 호출을 외관 기능에서 재편성 할 수 있습니다.

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

그런 다음 오류 처리가 정면을 호출하는 상위 기능으로 보이게하십시오.

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

이제 가정 해 봅시다 step3() 오류가 발생합니다. 부터 facade() 오류를 처리하지 않습니다 (존재합니다 아니요 On error resume next 안에 facade()), 오류가 반환됩니다 main() 그리고 step4() 그리고 step5() 실행되지 않습니다.

이제 오류 처리가 1 코드 블록에서 리팩토링되었습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top