On Error Resume Next

TIB發表於2010-01-23

QTP採用VBS作為指令碼語言,VBS中處理錯誤的語句是:

On Error Resume Next
On Error GoTo 0

 

If you don't use an On Error Resume Next statement anywhere in your code, any run-time error that occurs can cause an error message to be displayed and code execution stopped. However, the host running the code determines the exact behavior. The host can sometimes opt to handle such errors differently. In some cases, the script debugger may be invoked at the point of the error. In still other cases, there may be no apparent indication that any error occurred because the host does not to notify the user. Again, this is purely a function of how the host handles any errors that occur.

Within any particular procedure, an error is not necessarily fatal as long as error-handling is enabled somewhere along the call stack. If local error-handling is not enabled in a procedure and an error occurs, control is passed back through the call stack until a procedure with error-handling enabled is found and the error is handled at that point. If no procedure in the call stack is found to have error-handling enabled, an error message is displayed at that point and execution stops or the host handles the error as appropriate.

On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the On Error Resume Next statement. This allows execution to continue despite a run-time error. You can then build the error-handling routine inline within the procedure.

An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine. When a procedure is exited, the error-handling capability reverts to whatever error-handling was in place before entering the exited procedure.

Use On Error GoTo 0 to disable error handling if you have previously enabled it using On Error Resume Next.

The following example illustrates use of the On Error Resume Next statement.

On Error Resume Next
Err.Raise 6   ' Raise an overflow error.
MsgBox "Error # " & CStr(Err.Number) & " " & Err.Description
Err.Clear   ' Clear the error.

 

 

Err物件包含執行時的錯誤資訊。

Raise方法用於產生執行時錯誤

Clear方法用於清除執行時錯誤。

Description屬性儲存了錯誤相關的描述資訊。

Number屬性是指錯誤號。

Source屬性是指產生錯誤的源頭物件或應用程式名。

 

The Err object is an intrinsic object with global scope — there is no need to create an instance of it in your code. The properties of the Err object are set by the generator of an error — Visual Basic, an Automation object, or the VBScript programmer.

The default property of the Err object is Number. Err.Number contains an integer and can be used by an Automation object to return an SCODE.

When a run-time error occurs, the properties of the Err object are filled with information that uniquely identifies the error and information that can be used to handle it. To generate a run-time error in your code, use the Raise method.

The Err object's properties are reset to zero or zero-length strings ("") after an On Error Resume Next statement. The Clear method can be used to explicitly reset Err.

The following example illustrates use of the Err object:

On Error Resume Next
Err.Raise 6   ' Raise an overflow error.
MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear      ' Clear the error.

 

 

 

在QTP中除了使用VBS的On Error Resume Next來處理錯誤外,還可以採用以下兩種方式來處理:

1、設定Test Settings中的“When error occurs during run session”

 

By default, if an error occurs during the run session, QuickTest displays a popup message box describing the error. You must click a button on this message box to continue or end the run session.

You can accept the popup message box option or you can specify a different response by choosing one of the alternative options in the list in the When error occurs during run session box:

proceed to next action iteration. QuickTest proceeds to the next action iteration when an error occurs.
stop run. QuickTest stops the run session when an error occurs.
proceed to next step. QuickTest proceeds to the next step in the test when an error occurs.

QuickTest first performs any recovery scenarios associated with the test, and performs the option selected above only if the associated recovery scenarios do not resolve the error.

 

 

2、設定恢復場景(Recovery Scenario)

http://blog.csdn.net/Testing_is_believing/archive/2008/03/17/2193021.aspx

 

 

 

 

相關文章