Development Tip

"On Error Resume Next"문은 무엇을합니까?

yourdevel 2020. 12. 6. 22:12
반응형

"On Error Resume Next"문은 무엇을합니까?


몇 가지 VBScript 예제를 살펴 보았고 On Error Resume Next기본적으로 스크립트 시작 부분에있는 설명을 보았습니다 .

그것은 무엇을합니까?


기본적으로 오류가 발생하면 프로그램에 다음 줄에서 계속합니다.


On Error Resume Next효과가있는 경우에도 오류가 발생하면 Err 개체가 계속 채워 지므로 C 스타일 오류 처리를 계속 수행 할 수 있습니다.

On Error Resume Next

DangerousOperationThatCouldCauseErrors

If Err Then
    WScript.StdErr.WriteLine "error " & Err.Number
    WScript.Quit 1
End If

On Error GoTo 0

오류가 발생하면 스크립트를 중단하지 않고 다음 줄에서 실행이 계속됩니다.


이는 라인에서 오류가 발생하면 vbscript에게 스크립트를 중단하지 않고 계속 실행하도록 지시하는 것입니다. 때로는 코드 블록 에서 다음과 같이 실행 흐름을 변경하기 위해 레이블을 따라 On Error갑니다. 이제를 사용 하여 스파게티 코드가 생성되는 이유와 방법을 알 수 있습니다.GotoSubGOTO

하위 MySubRoutine ()
   오류시 ErrorHandler로 이동

   REM VB 코드 ...

   REM 더 많은 VB 코드 ...

Exit_MySubRoutine :

   REM 오류 처리기를 비활성화하십시오!

   오류시 0으로 이동

   REM 떠나다 ....
   서브 종료

ErrorHandler :

   REM 오류에 대한 조치

   Exit_MySubRoutine으로 이동
End Sub

On Error Statement-런타임 오류가 발생하면 제어가 명령문 바로 다음 명령문으로 이동하도록 지정합니다. Err 개체가 채워지는 방법 (Err.Number, Err.Count 등)


오류 처리를 활성화합니다. 다음은 부분적으로 https://msdn.microsoft.com/en-us/library/5hsw66as.aspx 에서 가져온 것입니다.

' Enable error handling. When a run-time error occurs, control goes to the statement 
' immediately following the statement where the error occurred, and execution
' continues from that point.
On Error Resume Next

SomeCodeHere

If Err.Number = 0 Then
    WScript.Echo "No Error in SomeCodeHere."
Else
  WScript.Echo "Error in SomeCodeHere: " & Err.Number & ", " & Err.Source & ", " & Err.Description
  ' Clear the error or you'll see it again when you test Err.Number
  Err.Clear
End If

SomeMoreCodeHere

If Err.Number <> 0 Then
  WScript.Echo "Error in SomeMoreCodeHere:" & Err.Number & ", " & Err.Source & ", " & Err.Description
  ' Clear the error or you'll see it again when you test Err.Number
  Err.Clear
End If

' Disables enabled error handler in the current procedure and resets it to Nothing.
On Error Goto 0

' There are also `On Error Goto -1`, which disables the enabled exception in the current 
' procedure and resets it to Nothing, and `On Error Goto line`, 
' which enables the error-handling routine that starts at the line specified in the 
' required line argument. The line argument is any line label or line number. If a run-time 
' error occurs, control branches to the specified line, making the error handler active. 
' The specified line must be in the same procedure as the On Error statement, 
' or a compile-time error will occur.

On Error Resume Next는 On Error, 다음 줄로 재개하여 재개 함을 의미합니다.

예를 들어 Try 블록을 시도하면 오류가 발생하면 스크립트가 중지됩니다.

참고 URL : https://stackoverflow.com/questions/2202869/what-does-the-on-error-resume-next-statement-do

반응형