Resume 语句

在错误处理例程完成后恢复执行。

建议尽可能在代码中使用结构化异常处理,而不是使用非结构化异常处理和 On Error 与 Resume 语句。 有关详细信息,请参阅 Try...Catch...Finally 语句。

语法

Resume [ Next | line ]  

组成部分

Resume
必需。 如果在错误处理程序所在的同一过程中发生错误,则使用导致错误的语句恢复执行。 如果已调用的过程中发生错误,则在包含错误处理例程的过程中最后调用的语句处恢复执行。

Next
可选。 如果在错误处理程序所在的同一过程中发生错误,则紧跟在使用导致错误的语句后面的语句恢复执行。 如果已调用的过程中发生错误,则使用紧跟在包含错误处理例程的过程中最后调用的语句后面的语句(或 On Error Resume Next 语句)恢复执行。

line
可选。 将在所需 line 参数中指定的行处恢复执行。 line 参数是行标签或行号,并且必须与错误处理程序位于同一过程中。

注解

注意

建议尽可能在代码中使用结构化异常处理,而不是使用非结构化异常处理和 On Error 与 Resume 语句。 有关详细信息,请参阅 Try...Catch...Finally 语句。

如果在错误处理例程之外的任何位置使用 Resume 语句,则会发生错误。

Resume 语句不能在包含 Try...Catch...Finally 语句的任何过程中使用。

示例

此示例使用 Resume 语句结束过程中的错误处理,然后使用导致错误的语句恢复执行。 会生成错误编号 55 来说明语句 Resume 的使用。

Sub ResumeStatementDemo()
  On Error GoTo ErrorHandler   ' Enable error-handling routine.
  Dim x As Integer = 32
  Dim y As Integer = 0
  Dim z As Integer
  z = x / y   ' Creates a divide by zero error
  Exit Sub   ' Exit Sub to avoid error handler.
ErrorHandler:     ' Error-handling routine.
  Select Case Err.Number   ' Evaluate error number.
      Case 6   ' "Divide by zero" error.
        y = 1 ' Sets the value of y to 1 and tries the calculation again.
      Case Else
        ' Handle other situations here....
  End Select
  Resume   ' Resume execution at same line
  ' that caused the error.
End Sub

要求

命名空间:Microsoft.VisualBasic

程序集:Visual Basic 运行时库(在 Microsoft.VisualBasic.dll 中)

另请参阅