web-dev-qa-db-ja.com

「On Error Resume Next」ステートメントは何をしますか?

いくつかのVBScriptの例を見に行きましたが、基本的にスクリプトの冒頭でOn Error Resume Nextというステートメントを見ました。

それは何をするためのものか?

58
Carlos Blanco

基本的には、エラーが発生すると次の行に進むだけでプログラムに通知します。

78
David

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
39
Tmdean

エラーが発生すると、スクリプトを中断することなく次の行で実行が継続されます。

24

これは、行でエラーが発生したときに、vbscriptにスクリプトを中止せずに実行を継続するよう指示していることを意味します。場合によっては、On ErrorGotoラベルの後に実行フローを変更するためにSubコードブロックのようなものが続きます。これで、GOTOの使用理由と使用方法がわかりますスパゲッティコードになる可能性があります:

 Sub MySubRoutine()
 On Error Goto ErrorHandler 
 
 REM VB code ... 
 
 REM More VB Code ... 
 
 Exit_MySubRoutine:
 
 REMエラーハンドラーを無効にします!
 
エラー時0 
 
 REM Leave .... 
終了Sub 
 
 ErrorHandler:
 
 REM Error 
 
 Goto Exit_MySubRoutine 
サブ終了
12
t0mm13b

On Error Statement-実行時エラーが発生すると、制御はステートメントの直後のステートメントに移ることを指定します。 Errオブジェクトが入力された回数(Err.Number、Err.Countなど)

3
Chandralal

エラー処理を有効にします。以下の一部は 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.
2
alfredo

On Error Resume Nextは、エラー時、次の行に再開して再開することを意味します。

例えばTryブロックを試すと、エラーが発生するとスクリプトが停止します

0
Coolvideos73