Micro Focus QTP (UFT) Forums
Error on On Error routine - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Error on On Error routine (/Thread-Error-on-On-Error-routine)



Error on On Error routine - upadhyay40 - 12-23-2009

Hello All,

I am trying to implement On Error Resume Next in my code but i got error on 2 line

The test run cannot continue due to a syntax error.
Syntax error
Line (1):
Code:
"On Error GoTo ErrorHandler: ' Enable error-handling routine.".

can any body please help me out to solve my problem.
My Code:
Code:
Public Sub OnErrorDemo()
On Error GoTo ErrorHandler: ' Enable error-handling routine.
Dim x,y,z
x = 32
y = 0
z = x / y ' Creates a divide by zero error
On Error GoTo 0 ' Turn off error trapping.
On Error Resume Next ' Defer error trapping.
z = x / y ' Creates a divide by zero error again
If Err.Number = 6 Then
' Tell user what happened. Then clear the Err object.
Dim Msg As String
Msg = "There was an error attempting to divide by zero!"
MsgBox(Msg, , "Divide by zero error")
Err.Clear() ' Clear Err object fields.
End If
Exit Sub ' Exit to avoid handler.
ErrorHandler: ' Error-handling routine.
Select Case Err.Number ' Evaluate error number.
Case 6 ' Divide by zero error
MsgBox("You attempted to divide by zero!")
' Insert code to handle this error
Case Else
' Insert code to handle other situations here...
End Select
Resume Next ' Resume execution at same line
' that caused the error
End Sub

Thanks
Mahesh


RE: Error on On Error routine - Saket - 12-24-2009

Hi Mahesh,
'On Error GoTo ErrorHandler' is not supported in QTP so you can not use the statement here.
you can use only 'on error resume next' or 'on error goto 0'
@Everyone - Please put your codes in proper tags, which makes easier to read through all the statements.
Refer Help (top right of this page)


RE: Error on On Error routine - jsknight1969 - 12-29-2009

You can use the Err object in an if/case statement to handle error.

Code:
on error resume next
z = 1/0
select case err.number
   case -21345896 'please look up the correct error number
     msgbox err.description & ": you divided by zero"
   case else
      msgbox err.description
end select

I created a class to handle error and enhanced the class with a DLL written in .NET to store to eventlog, file, or database. You don't need .NET for file or database, I just prefer the eventlog and could write it easily in .NET. To use the class just replace the case statement with a sub call and pass the err object.