Thursday, 8 October 2015

Error Handling

It is preventive method where we  will specify set of instructions at some places in code , where we think error might occurs . there are three types of errors that are possible to encounter while working with QTP. They are
  • Syntax Errors
  • Logical Errors
  • Run Time Errors

Syntax Errors: Syntax errors are the typos or a piece of the code that does not confirm with the VBscripting language grammar. Syntax errors occur at the time of compilation of code and cannot be executed until the errors are fixed. These errors in VBscript can be avoided by
  • Properly reviewing the Code
  • Using Option Explicit statement ,and
  • We can also follow standard coding conventions , like indentation commenting ,etc.,

Example:
Option explicit
Dim a,b,c
a= cint(input box(“Enter First value”)
b= cint(input box(“Enter second value”)
c=a+b
msgbox “addition of two numbers is “&c

Logical Errors: If the script is syntactically correct but it produces unexpected results, then that can be predicted as logical errors. Logical error usually does not interrupt the execution but produces incorrect results. Logical errors can be occured due to variety of reasons.
  • Wrong assumptions or misunderstanding of the requirement.
  • Sometimes incorrect program logics(using do-while instead of do-Until) or Infinite Loops.
  • One of the ways to detect a logical error is to perform peer reviews and also verifying the QTP output file/result file to ensure the tool has performed what it has intended to do.

Run Time Errors: As The name states, this kind of Error happens during Run Time. The reason for such kind of errors is that the script trying to perform something but it is unable to do so and the script usually stops as it is unable to continue with the execution. Classic Examples for Run Time Errors are,
  • File NOT found but the script trying to read the file.
  • Object NOT found but script is trying to act on that particular object.
  • Dividing a number by Zero.
  • Array Index out of bounds while accessing array elements.

One way of handling the errors in VbScript is to trap them using ’On Error Resume Next”. Once the error is traped we can identify the error details using Err object.
On Error Resume Next: 
This statement will simply suppress the errors and executes entire script. Suppressing the error means when error occur VBScript shows popup error message, pauses execution and waits for user action. When using this statement, the error popup will not be displayed on failure and goes to the next statement for execution.
On Error Go to 0: This statement is used to disable “On Error Resume Next”.
In VBScript there is no facility to call a statement/function on error. But you can use err object to perform condition based error handling. On Error Resume Next, Go to 0 and Err objects will work for VBScript as well as for QTP Objects.
Err Object: This object captures the information about runtime errors. Using this you can always get the last error details. To access error details you can. It is used to interpret the cause and context of the error condition . The error object is global scope and always available in our code.
Methods of Err Object  are we can use err.number or err.description or source property statements. We can clear and raise the runtime errors using this object.

Number Property: Specifies the error number assigned to the error that occurs, when error number is greater than 0.
Syntax: Msgbox err.number
Description Property: This property provides the description of the error that occurs for the end users to learn about the error. It is useful when there is a possibility of human error while using an application. IT helps the user to understand his or her mistake in interacting the application and correct it accordingly.
Syntax: Msgbox err.description
Source Property: It is description that provides the name of the object that cause the error condition.
Syntax: Msgbox err.source




No comments:

Post a Comment