Information.Err 方法

定义

包含运行时错误的相关信息。Contains information about run-time errors.

public:
 static Microsoft::VisualBasic::ErrObject ^ Err();
public static Microsoft.VisualBasic.ErrObject Err ();
static member Err : unit -> Microsoft.VisualBasic.ErrObject
Public Function Err () As ErrObject

返回

ErrObject

包含运行时错误的相关信息。Contains information about run-time errors.

示例

此示例使用 Err 构造错误消息对话框中的对象的属性。This example uses the properties of the Err object in constructing an error-message dialog box. 请注意,如果您 Clear 首先使用方法,当您使用方法生成 Visual Basic 错误时 Raise ,Visual Basic 的默认值将成为该对象的属性 ErrNotice that if you use the Clear method first, when you generate a Visual Basic error with the Raise method, Visual Basic's default values become the properties of the Err object.

Dim Msg As String
' If an error occurs, construct an error message.
On Error Resume Next   ' Defer error handling.
Err.Clear()
Err.Raise(6)   ' Generate an "Overflow" error.
' Check for error, then show message.
If Err.Number <> 0 Then
  Msg = "Error # " & Str(Err.Number) & " was generated by " &
        Err.Source & ControlChars.CrLf & Err.Description
  MsgBox(Msg, MsgBoxStyle.Information, "Error")
End If

注解

对象的属性 Err 由错误 Visual Basic、对象或程序员的生成器设置。The properties of the Err object are set by the generator of an error - Visual Basic, an object, or the programmer.

发生运行时错误时,将 Err 使用唯一标识错误的信息填充对象的属性,并使用可用于处理错误的信息。When a run-time error occurs, the properties of the Err object are filled with information that uniquely identifies the error and that you can use to handle the error. 若要在代码中生成运行时错误,请使用 Raise 方法。To generate a run-time error in your code, use the Raise method.

Err对象的属性重置为零或长度为零的字符串 ( Exit Sub Exit Function Exit Property Resume Next 在错误处理例程中的、、或语句后 ) ""。The Err object's properties are reset to zero or zero-length strings ("") after an Exit Sub, Exit Function, Exit Property, or Resume Next statement within an error-handling routine. Resume 错误处理例程之外使用语句的任何形式都不会重置该 Err 对象的属性。Using any form of the Resume statement outside of an error-handling routine will not reset the Err object's properties. 您可以使用 Clear 方法显式重置 ErrYou can use the Clear method to explicitly reset Err.

使用 Raise 方法而不是 Error 语句来生成系统错误和类模块的运行时错误。Use the Raise method rather than the Error statement to generate run-time errors for system errors and class modules. 您决定是否 Raise 在其他代码中使用方法取决于您要返回的信息的丰富程度。Your decision about whether to use the Raise method in other code depends on the richness of the information you want to return.

Err对象是具有全局范围的内部对象。The Err object is an intrinsic object with global scope. 因此,您无需在代码中创建它的实例。Therefore, you do not need to create an instance of it in your code.

适用于