Qtp uses vbs as the script language. The statements used to handle errors in vbs are:
On Error resume next
On Error goto 0
If you don't useOn Error resume nextStatement 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 handle y the user. again, this is purely a function of how the host handles any errors that occur.
Within any particle 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 nextCauses 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 containingOn Error resume nextStatement. This allows execution to continue despite a run-time error. You can then build the error-handling routine inline within the procedure.
AnOn Error resume nextStatement becomes inactive when another procedure is called, so you should executeOn Error resume nextStatement 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.
UseOn Error goto 0To disable error handling if you have previusly enabled it usingOn Error resume next.
The following example extends strates use ofOn Error resume nextStatement.
On Error Resume NextErr.Raise 6 ' Raise an overflow error.MsgBox "Error # " & CStr(Err.Number) & " " & Err.DescriptionErr.Clear ' Clear the error.
The ERR Object contains the error message during running.
The raise method is used to generate runtime errors.
The clear method is used to clear running errors.
The description Property stores error-related descriptions.
The number attribute indicates the error number.
The source attribute refers to the source object or application name that produces an error.
TheErrObject is an intrinsic object with global scope-there is no need to create an instance of it in your code. The properties ofErrObject are set by the generator of an error-Visual Basic, an automation object, or the VBScript programmer.
The default property ofErrObject isNumber.Err. NumberContains an integer and can be used by an Automation Object to return an scode.
When a run-time error occurs, the properties ofErrObject 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, useRaiseMethod.
TheErrObject's properties are reset to zero or zero-length strings ("") afterOn Error resume NextStatement.ClearMethod can be used to explicitly ResetErr.
The following example extends strates use ofErrObject:
On Error Resume NextErr.Raise
6 ' Raise an overflow error.MsgBox ("Error # " & CStr(Err.Number
) & " " & Err.Description
)Err.Clear
' Clear the error.
In qtp, besides using the on error resume next of vbs to handle errors, you can also use the following two methods:
1. Set "When error occurs during run session" in test settings"
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 acceptPopup message boxOption or you can specify a different response by choosing one of the alternative options in the list inWhen error occurs during run sessionBox:
- 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. Set the recovery scenario (recovery scenario)
Http://blog.csdn.net/Testing_is_believing/archive/2008/03/17/2193021.aspx